我想用下面的代碼添加一個新的方法到jQuery驗證插件。我的目標是檢查電子郵件地址是否已經存在於數據庫(MySQL)中。如果是,它會通知用戶註冊另一個電子郵件地址。不知何故,總是返回的結果是「電子郵件已被採納」。jQuery驗證插件addMethod
這些都是validate.js代碼:
$(document).ready(function(){
$.validator.addMethod("uniqueEmail", function(value, element) {
$.ajax({
type: "POST",
url: "availability.php",
data: value,
success: function(exist)
{
if(exist>0) {
return true;
} return false;
}
});
} ,"Email is already taken");
$('#signup form').validate({
rules: {
firstname: {
required: true,
minlength:3
},
lastname: {
required: true,
minlength: 3
},
affiliation: {
required: true,
},
occupation: {
required: true,
},
email: {
required: true,
email: true,
uniqueEmail: true
},
password: {
minlength: 6,
required: true
},
repassword: {
equalTo: "#password"
}
},
messages: {
firstname: {
minlength: "Your first name should be more than 3 characters"
},
lastname: {
minlength: "Your last name should be more than 3 characters"
},
},
success: function(label) {
label.text('OK!').addClass('valid');
}
});
});
而這些都是在我的PHP文件中的代碼:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/magicquotes.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/db.inc.php';
$email = strtolower($_POST['email']);
$email = mysqli_real_escape_string($link, $email);
$sql = "SELECT * FROM bradduser WHERE email='$email'";
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
echo $num;
?>
對於這個例子有問題的人,我遇到了服務器端語言的響應問題,所以我設法通過直接在插件中從'ajax'的'dataType'來解決它'dataType:「json」'到'dataType:「text」'on line 1206' – CIRCLE 2014-05-29 11:48:07