当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - jQuery Validation remote: to Prevent a Duplicate Entry is Not Working

问题描述:

I am attempting to create a validation rule using jQuery Validation that checks the database for an existing Customer Name and prevents a duplicate Customer Name from being inserted into the database. It seems that remote: is a straight forward option if I could get it to work. With remote included, the form does not submit but all other validation works as expected. Once remote: is removed the form submits and all validation runs perfect. I have looked every where and I am out of ideas. Thank you.

< 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>

分享给朋友:
您可能感兴趣的文章:
随机阅读: