[英]how to check that ajax returns true or false value using jquery
i'm building a signup form for my website i validate my signup page with jquery i want to do that when all the fields are filled with valid way then the signup page redirect or load and store the data in database... 我正在为我的网站构建一个注册表单,我使用jquery验证我的注册页面,当所有字段都以有效方式填充时,我想这样做,然后注册页面重定向或加载并将数据存储在数据库中...
1. first jquery code checks input fields 1.第一个jquery代码检查输入字段
2. then in the jquery code there is a ajax call which check if the email already exist or not if the ajax call return true then data will be inserted in database if it return false the the page will not load and display the message that email is already exist 2.然后在jquery代码中有一个ajax调用,它检查电子邮件是否已经存在,如果ajax调用返回true,则将数据插入数据库,如果返回false,则页面将不会加载并显示该电子邮件的消息已经存在
the problem in my code is that when the ajax call is true or false my page keep loading and insert the data in database but i want that if value is false page will not load. 我的代码中的问题是,当ajax调用为true或false时,我的页面会继续加载并在数据库中插入数据,但我希望如果value为false,页面将不会加载。
here is my code 这是我的代码
$.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;
}
}
});
and here is my email_validator.php 这是我的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);
}
You can add async option to false and return outside the ajax call: 您可以将async选项添加到false并在ajax调用之外返回:
function testAjax() {
var resultStatus = "";
$.ajax({
type:'POST',
url:'email_validator.php',
async: false,
data:{email:mail},
success:function (response){
var result = $.parseJSON(response);
resultStatus = result.status;
if (result.status===false) {
$('#error').html("Email alreaday exist");
}
}
});
return resultStatus;
}
[UPDATE] [更新]
But you do not need to check this on client side. 但是您不需要在客户端上检查。 Yo can check if email exists in your
email_validator.php
and immediately write it to the database if it does not exist: 您可以检查您的
email_validator.php
是否存在电子邮件,如果不存在则立即将其写入数据库:
email_validator.php 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;
// --> INSERT the new email into the database
}
echo json_encode($result);
exit(0);
}
Try this: 尝试这个:
$.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;
}
}
});
In my opinion you should invoke the PHP API method to insert the new email directly inside the success function callback: 我认为您应该调用PHP API方法将新电子邮件直接插入成功函数回调中:
$.ajax({
type: 'POST',
url: 'email_validator.php',
data: {email: mail},
success: function (response) {
var result = $.parseJSON(response);
if (result.status === false) {
$('#error').html("Email already exists");
}
else (result.status === true) {
// -> Call here the PHP api method to insert the new email in the database
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.