我正在为我的网站构建一个注册表单,我使用 jquery 验证我的注册页面,我想在所有字段都以有效方式填充时执行此操作,然后注册页面重定向或加载并将数据存储在数据库中...
1.第一个 jquery 代码检查输入字段
2. 然后在 jquery 代码中有一个 ajax 调用,它检查电子邮件是否已经存在,如果 ajax 调用返回 true,则如果返回 false,则数据将插入数据库中页面将不会加载并显示电子邮件已存在的消息
我的代码中的问题是,当 ajax 调用为 true 或 false 时,我的页面继续加载并将数据插入数据库中,但我希望如果值为 false 页面将不会加载。
这是我的代码
$.ajax({
type:'POST',
url:'email_validator.php',
data:{email:mail},
success:function (response){
var result = $.parseJSON(response);
if (result.status===false) {
$('#error').html("Email alreaday exist");
return false;
} if(result.status===true) {
return true;
}
}
});
这是我的 email_validator.php
<?php
if(isset($_POST["email"])){
$result = array();
$email=$_POST["email"];
$db=new PDO("mysql:host=localhost;dbname=the_scops","root","");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH=$db->prepare("SELECT email FROM signup WHERE email=?");
$STH->execute([$email]);
if($STH->rowCount() == 1){
//echo "Email alreday exist";
$result["status"] = false;
}
else{
$result["status"] = true;
}
echo json_encode($result);
exit(0);
}
最佳答案
试试这个:
$.ajax({
type:'POST',
url:'email_validator.php',
data:{email:mail},
success:function (response){
if (response.status === true) {
return true;
} else {
$('#error').html("Email alreaday exist");
return false;
}
}
});
关于javascript - 如何使用 jquery 检查 ajax 返回 true 或 false 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45904357/