【问题标题】:How to find the mobile duplication in form using php如何使用php在表单中查找移动重复项
【发布时间】:2016-06-16 07:05:09
【问题描述】:

我已经创建了带有选择选项的表单。根据选择,我显示和隐藏表单字段.... 如何验证手机号码。如果用户再次输入相同的手机号码,则抛出错误消息。如何找到移动复制条目

$(document).ready(function() {
 $('#hidden-div').hide();
 $("#select_btn").change(function() {
    toggleFields();
  });
  });

function toggleFields() {
  var selectVal = $("#select_btn").val();
  if (selectVal <= 5) {
    $hiddenHtml = $('#hidden-div').clone().html();
    $("#refer").html('');
    for (var i = 0; i < selectVal; i++) {
      $("#refer").append($hiddenHtml);
    }
  }
}

function InvalidMsg(textbox) {

     if(textbox.validity.patternMismatch){
        textbox.setCustomValidity('please enter valid mobile number');
    }    
    else {
        textbox.setCustomValidity('');
    }
    return true;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id ="refer-form" name="refer-form"   method="post" >
 
  <p>No of Referrer:
    <select id="select_btn" onchange="toggleFields();">
      <option value="0">--Select--</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <div id="hidden-div">
 <div id="text">Referrer</div>
      <p>Name:
        <input type="text" name="name[]" class="name"/>
      </p>
      <p>Mobile:
        <input type="text" name="mobile[]" class="mobile" oninvalid="InvalidMsg(this);" />
      </p>
      <p>Email:
        <input type="text" name="email[]" />
      </p>
      
    </div>
    <div id="refer">

    </div>
    <p align="center">
      <input type="submit" value="Submit" />
    </p>
</form>

【问题讨论】:

    标签: javascript php jquery html forms


    【解决方案1】:

    你必须在输入名称中使用数组

    喜欢这个

          <p>Name:
            <input type="text" name="reference_name[]" />
          </p>
          <p>Mobile:
            <input type="text" name="reference_mobile[]" />
          </p>
          <p>Email:
            <input type="text" name="reference_email[]" />
          </p>
    

    【讨论】:

    • 你会得到每个元素的值数组
    • 嗨,阿米特,我需要像数组这样的输出([player_name] => uma [mobile] => 9769956198 [player_email] => ggdfhgd [reference_name] => sakthi2 [reference_mobile] => 989898598859 [reference_email] => hjgjhgjh [City] => Chennai [Course] => B.A).
    • 当我按照你所说的那样更改代码时......我得到了数组值......数组([player_name] => sakthi [mobile] => 8888888888 [player_email] => ggdfhgd [reference_name ] => 数组 ( [0] => [1] => sakthi1 [2] => uma1t ) [reference_mobile] => 数组 ( [0] => [1] => 9769956199 [2] => 9769956190 ) [reference_email ] => 数组 ( [0] => [1] => gfhfh [2] => gfhfh ) [城市] => 数组 ( [0] => 0 [1] => 孟买 [2] => 孟买 ) [课程] => 数组 ( [0] => B.com [1] => B.com [2] => MBA ) )
    • 是的,这仅适用于您隐藏的 div 值,您将获得它的数组。就像在你的 html 中你选择两个或三个然后两次创建的 div 所以你必须为它们循环迭代并从中获取价值,这就是为什么建议把每个元素的数组都做好..在 forloop 中你必须计算任何一个具有数组和驱动循环的输入从计数尝试它
    【解决方案2】:

    看看你的代码,你所有的名字都不一样。

    您想使用 javascript 还是 php 打印它们?
    如果是 php,则需要在表单中添加一个操作。提交表单时,所有值都将发送到您的操作文件,您将能够使用 $_POST 获取它们。

    因此,假设您的文件名为 join.php。您的表格将是 &lt;form method="POST" action="join.php"&gt;,它会在提交时调用自己。
    然后您将能够使用 print_r($_POST); 获取值

    【讨论】:

      【解决方案3】:

      您可以使用数组来保存 .mobile 类的所有输入值。如果您发现重复项,您可以返回错误。

      Demo

      $("#refer-form").on('submit', function(e){
        e.preventDefault();
        mobnos = new Array();
        $(".mobile").each(function() {
          $(this).removeClass('duplicate');
          val = $(this).val();
          if($.inArray(val, mobnos) > 0){
            $(this).addClass('duplicate');   
          } else {
           mobnos.push(val);
          }
        });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-03
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多