Javascript jQuery验证远程重定向页面为false
我正在使用jqueryvalidateremote检查一个php文件,看看是否有电子邮件地址已经在使用中。它的工作很好,但是,我需要它重定向到一个新的页面,如果电子邮件键入已经存在或“假”。下面是正在工作的远程验证脚本和简单的php文件。同样,这很好,只是不知道如果php脚本返回false,如何重定向到新页面Javascript jQuery验证远程重定向页面为false,javascript,php,jquery,redirect,jquery-validate,Javascript,Php,Jquery,Redirect,Jquery Validate,我正在使用jqueryvalidateremote检查一个php文件,看看是否有电子邮件地址已经在使用中。它的工作很好,但是,我需要它重定向到一个新的页面,如果电子邮件键入已经存在或“假”。下面是正在工作的远程验证脚本和简单的php文件。同样,这很好,只是不知道如果php脚本返回false,如何重定向到新页面 rules: { fullname: "required", email: { required:
rules: {
fullname: "required",
email: {
required: true,
email: true,
remote: {
url: "validate-email.php",
type: "post",
},
},
messages: {
email: {
required: 'Please enter a valid email address',
remote: 'Sorry, this email address is already in use',
}
}
validate-email.php
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
// A user with this email address already exists
echo 'false';
}else{
echo 'true';
}
}
最后再提一次,上面的php和jquery脚本工作得很好。如果php结果为echo'false',只需要重定向到新页面 我可以通过简单地添加来自IntyMate的信息来重定向
rules: {
fullname: "required",
email: {
required: true,
email: true,
remote: {
url: "validate-email.php",
type: "post",
success: function(e) { if(e == false){window.location.replace("http://website");}}
},
},
messages: {
email: {
required: 'Please enter a valid email address',
remote: 'Sorry, this email address is already in use',
}
}
也许会有帮助?这应该允许您检索服务器响应。实际上,您需要添加一个父级:
success:function(e){}
。e变量是服务器的响应(即:“true”或“false”)。在这种情况下,如果(e==“true”){}我不理解这一点,您可以执行if(e==“true”){}
。验证插件的全部目的是在表单无效时停止表单的提交。如果将false
回送到remote
方法,则这是一个“无效”表单,在用户修复其输入值之前不应提交。因此,既然您不关心用户修复数据和提交表单,为什么不使用PHP重定向到一个新页面呢