【发布时间】:2016-10-17 09:47:42
【问题描述】:
我正在使用 jQuery Validator Plugin 来验证简报表单注册。 根据文档和其他帖子,我正在使用远程验证方法来检查电子邮件是否已存在于数据库中。
该方法有效。如果我输入现有地址,将显示远程消息,我将无法发送表格。 但是,如果我将电子邮件地址更改为不存在的电子邮件地址,远程消息不会被删除,我将无法发送表单。
我错过了什么吗?
这是我的 jQuery:
$("#form").validate({
rules: {
email: {
required: true,
email: true,
remote: {
url: ajaxurl,
data: {
action: 'my_action'
},
type: "post"
}
}
},
messages: {
email: {
required: "This field is required.",
email: "Please enter a valid email address.",
remote: "This Email address is already in use."
}
},
});
这是我的 ajax 函数(wordpress):
function my_action(){
if ( !empty( $_POST['email'] ) ) {
$emailArray = get_meta_values( 'email', 'my_post_type' );
if (in_array( $emailArray, $_POST['email'] ) ) {
echo "notexist";
}
else {
echo 'exist';
}
}
}
【问题讨论】:
-
在
my_action()函数末尾添加exit;。 -
这似乎无法解决我的问题。请问您在
my_action()末尾添加exit;到底有什么作用? -
好吧,它停止了脚本的流程,因为它是一个异步请求。在某些情况下,它不是必需的,但在使用 Wordpress 进行开发时使用它是一种很好的做法。
-
感谢您的信息!
标签: jquery ajax wordpress forms validation