jQuery验证远程:防止重复条目不起作用

时间:2016-05-27 06:02:46

标签: javascript jquery validation

我正在尝试使用jQuery Validation创建验证规则,该规则检查数据库中是否存在现有客户名称,并防止将重复的客户名称插入到数据库中。看起来远程:如果我可以让它工作,这是一个直接的选择。如果包含远程,表单不会提交,但所有其他验证都按预期工作。一旦远程:删除表单提交,所有验证运行完美。我已经看过每一个地方而且我没有想法。谢谢。

< script >

  $('#new_customer_form').validate({
    rules: {
      customer_form: {
        required: true,
        remote: {
          url: 'php/customer_insert_checkDuplicate.php',
          type: 'post',
          dataType: 'json',
          data: {
            customer_form: function() {
              return $('#customer_form').val();
            }
          }
        }
      },
      rNumber_form: {
        digits: true
      }
    },
    messages: {
      rNumber_form: {
        digits: 'Please enter numbers only.'
      },
      customer_form: {
        required: 'Please enter a Customer Name.',
        remote: 'Customer Name is already in the Database.'
      }
    },
    submitHandler: function(form) {
      $('#submit_button').attr('disabled', 'disabled');
      $.ajax({
        url: "php/customer_insert.php",
        type: "post",
        data: $('#new_customer_form').serialize(),
        success: function() {
          $.bigBox({
            title: '<i class="fa fa-lg fa-fw fa-user"></i>' + $('#customer_form').val() + ' Created',
            content: '',
            color: "#3276B1",
            icon: "fa fa-thumbs-up bounce animated",
            timeout: 1500
          });
        }
      });
    }
  }); //end validation

< /script>
<?php

include ('../php/database.php');
$customer_form = filter_input(INPUT_POST, 'customer_form');

function check_customer_duplicates($customer_form){
    global $db;
    $check_customer = "SELECT customer FROM customer WHERE customer = '$customer_form'";
    $check = $db->prepare($check_customer);
    $check -> execute();
    $customer_count = $check->fetchAll();
    $check->closeCursor();
    
    $count = count($customer_count);
    if ($count>0){
        echo json_encode('false');
    }else{
        echo json_encode('true');
    }
}
?>
<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <h1>
            <i class="fa fa-lg fa-fw fa-user"></i>
            Create Customer
        </h1>

  </div>
</div>

<div class="jarviswidget col-xs-12 col-sm-12 col-md-12 col-lg-6" id="wid-id-1">
  <header>
    <span class="widget-icon"> <i class="fa fa-edit"></i> </span>
    <h2>
            New Customer 
        </h2>	
  </header>

  <!-- widget div-->
  <div>
    <!-- widget content -->
    <div class="widget-body no-padding">
      <form name="new_customer_form" id="new_customer_form" class="smart-form" novalidate="novalidate" method="post">

        <fieldset>
          <div class="row">
            <section class="col col-6">
              <label class="input"> <i class="icon-prepend fa fa-book"></i>
                <input type="text" name="rNumber_form" id="rNumber_form" placeholder="Registration Number" autocomplete="off">
              </label>
            </section>
          </div>

          <div class="row">
            <section class="col col-6">
              <label class="input"> <i class="icon-prepend fa fa-user"></i>
                <input type="text" name="customer_form" id="customer_form" placeholder="Customer" autocomplete="off">
              </label>
            </section>
            <section class="col col-6">
              <label class="input"> <i class="icon-prepend fa fa-book"></i>
                <input type="text" name="registrant_form" id="registrant_form" placeholder="Registrant Name" autocomplete="off">
              </label>
            </section>
          </div>
          <div class="row">
            <section class="col col-6">
              <label class="input"> <i class="icon-prepend fa fa-fax"></i>
                <input type="tel" name="customer_fax_form" id="customer_fax_form" placeholder="Fax" data-mask="(999) 999-9999" autocomplete="off">
              </label>
            </section>

            <section class="col col-6">
              <label class="input"> <i class="icon-prepend fa fa-user"></i>
                <input type="email" name="customer_email_form" id="customer_email_form" placeholder="e-mail" autocomplete="off">
              </label>
            </section>
          </div>


        </fieldset>
        <footer>
          <input type="submit" value="Save" id="submit_button" class="btn btn-primary">
        </footer>
      </form>
    </div>
  </div>
</div>

0 个答案:

没有答案