Jqueryvalidation插件 – 检查电子邮件是否存在

我正在尝试检查电子邮件字段模糊,如果电子邮件已存在于db中。 我的代码现在是:

 $(document).ready(function () { // Validation $("#soutez").validate({ rules: { email: { required: true, email: true, remote: "check-email.php", }, }, messages:{ email:'Email address exists.' }, onkeyup: false, onblur: true, }); });  

而PHP代码是

 $email= $_GET['email']; echo $email; $path = $_SERVER['DOCUMENT_ROOT']; include_once $path . '/wp-config.php'; include_once $path . '/wp-load.php'; include_once $path . '/wp-includes/wp-db.php'; include_once $path . '/wp-includes/pluggable.php'; global $wpdb; $email_exists = $wpdb->get_row('SELECT COUNT(*) as count from reg_form_new WHERE email = "'.$email.'"'); if ( $email_exists->count == 0 ) { echo 'true'; } else { echo 'false'; } exit; } 

php代码正确返回true / false,但由于某种原因它不能与jQuery脚本一起使用。 谁能告诉我我做错了什么? 谢谢

如果你想让它通过,你必须完全回显字符串'true'而没有别的。 其他任何内容( 'false'nullundefined或任何字符串)将被解释为无效,如果有字符串,则会显示该字符串。

因此,请仔细检查您的PHP脚本现在只打印'true''false' 。 您显示的脚本会打印电子邮件,后跟'true''false' 。 这将始终被解释为false

阅读远程方法的文档:

响应被评估为JSON,对于有效元素必须为true,并且对于无效元素,可以是任何false,undefined或null,使用默认消息; 或者一个字符串,例如。 “该名称已被采用,请尝试使用peter123”以显示为错误消息。

使用Firebug或Chrome Inspectorvalidation您的回复是否正确。

我不是Jquery的专家,你可以根据你的需要调整工作function:

 $.ajax({ url: "check-email.php", type:'GET', /*you my specify dataType: 'json', for example ...*/ data: $("#soutez").serialize(), //email input id success: function(res) { switch(res) { case ('true'): alert("true"); break; case ('false'): alert('false'); break; // other cases... } } });