问题使用XMLHtt prequest后返回false [英] Problem returning false after using XMLHttpRequest
问题描述
我发现这个<一href="http://stackoverflow.com/questions/3860371/always-return-true-in-first-call-returns-correct-value-after-first-call">Related进入和其他几个人,但我还没有完全找到了我的答案,所以我张贴新的。我想用我的理解阿贾克斯的运行从Javascript数据库查询。我需要返回false如果电子邮件已经在数据库,并返回true,如果没有,但我似乎无法找出什么我做错了。我已经张贴了我的第一次尝试之前,我开始插科打诨,使之乱。
I have found this Related entry and a few others but I have not quite found my answer so I am posting new. I am trying to run a database query from Javascript using what I understand of Ajax. I need to return false if the email is already in the database and return true if it does not, but I cannot seem to figure out what I am doing wrong. I have posted my first attempt before I started messing around and making it messy.
JavaScript的:它似乎无论什么返回true
javascript: It seems to return true no matter what.
function emailvalid(){
var email = document.getElementById('email').value;
var confirmemail = document.getElementById('cemail').value;
if(email != '' && confirmemail != '')
{
if(email == '')
{
document.getElementById('email').style.backgroundColor ='red';
document.getElementById('tdemail').style.color = 'red';
document.getElementById('tdemail').innerHTML = 'You must enter a valid email address.';
document.getElementById('tdcemail').innerHTML = '';
document.getElementById('cemail').value = '';
return false;
}
xmlhttp = new XMLHttpRequest();
var url="phpfiles/checkemail.php";
url = url+"?email="+email+"&cemail="+confirmemail;
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4)
{
var answer = xmlhttp.responseText;
if(answer == 'r0')
{
document.getElementById('email').style.backgroundColor ='green';
document.getElementById('tdemail').style.color = 'red';
document.getElementById('tdemail').innerHTML = 'This email address is already associated with an exiting account. Please login or use a different email address.';
document.getElementById('tdcemail').innerHTML = '';
document.getElementById('cemail').value = '';
return false;
}
else if(answer == 'r1')
{
document.getElementById('tdemail').style.color = 'green';
document.getElementById('email').style.backgroundColor ='green';
document.getElementById('tdemail').innerHTML = "Emails match and are valid";
document.getElementById('tdcemail').style.color = 'green';
document.getElementById('tdcemail').innerHTML = "Emails match and are valid";
document.getElementById('cemail').style.backgroundColor ='green';
return true;
}
else if(answer == 'r2')
{
document.getElementById('tdemail').style.color = 'red';
document.getElementById('email').style.backgroundColor ='red';
document.getElementById('tdemail').innerHTML = "Email is not valid";
document.getElementById('tdcemail').style.color = 'red';
document.getElementById('tdcemail').innerHTML = "Email is not valid.";
document.getElementById('cemail').style.backgroundColor ='red';
return false;
}
else if(answer == 'r3')
{
document.getElementById('tdemail').style.color = 'green';
document.getElementById('email').style.backgroundColor ='green';
document.getElementById('tdemail').innerHTML = "Email is valid";
document.getElementById('tdcemail').style.color = 'red';
document.getElementById('tdcemail').innerHTML = "Emails do not match";
document.getElementById('cemail').style.backgroundColor ='red';
return false;
}
else if(answer == 'r4')
{
document.getElementById('tdemail').style.color = 'red';
document.getElementById('email').style.backgroundColor ='red';
document.getElementById('tdemail').innerHTML = 'Please enter a valid email address.';
document.getElementById('tdcemail').style.color = 'red';
document.getElementById('tdcemail').innerHTML = 'Please enter a valid email address.';
document.getElementById('cemail').style.backgroundColor ='red';
return false;
}
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
document.getElementById('tdemail').style.color = 'red';
document.getElementById('email').style.backgroundColor ='red';
document.getElementById('tdemail').innerHTML = "Email is not valid";
document.getElementById('tdcemail').style.color = 'red';
document.getElementById('tdcemail').innerHTML = "Email is not valid.";
document.getElementById('cemail').style.backgroundColor ='red';
return false;
}
}
的innerHTML的东西工作得很好,这也是为什么我很困惑。
The innerHTML stuff works just fine which is also why I am confused.
PHP:工作正常,据我可以告诉
php: works fine as far as I can tell.
<?php
$email = $_GET['email'];
$cemail = $_GET['cemail'];
$emailvalidstring = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
include('connection.php');
$checkemailquery = "SELECT email FROM users WHERE email='".$email."'";
$checkemailresult = mysql_query($checkemailquery);
//if querey failed.
if(!$checkemailresult)
{
$error = mysql_error();
print $error;
exit;
}
//if the query did run, the email exists. Print error and exit.
if(mysql_affected_rows() != 0)
{
echo "r0";
return false;
}
else{
if(preg_match($emailvalidstring, $email) && $email == $cemail)
{
echo "r1";
return true;
}
else if(!preg_match($emailvalidstring, $email) && $email == $cemail)
{
echo "r2";
return false;
}
else if(preg_match($emailvalidstring, $email) && $email != $cemail)
{
echo "r3";
return false;
}
else if(!preg_match($emailvalidstring, $email) && $email != $cemail)
{
echo "r4";
return false;
}
}
?>
HTML:我把它做了仔细检查,一旦以提醒用户,如果它已被使用,再进行第二次检查服务器上,以确保它仍然是真实的。 cemail的确认电子邮件。
html: I have it do a double check, once to alert the user if it is already taken, and then a second check on the server to make sure it is still true. cemail is the confirmation email.
<form id="newaccount" name="newaccount" method="post" action="phpfiles/accountcode.php" onSubmit="return emailvalid()" >
<input type="text" id="email" onBlur="emailvalid()"/>
<input type="text" id="cemail" onBlur="emailvalid()" />
<input type="submit" value="New"/>
</form>
感谢大家的帮助。我希望我已经把银子不够好。我愿意接受任何想法,我宁愿在这个新的。
Thank you all for your help. I hope I have laid it out well enough. I am open to ANY ideas as I am rather new at this.
推荐答案
您正在返回假
在匿名的回调函数不是在 emailValid
功能。只需要声明一个全局布尔变量,并在匿名的回调函数相应地改变它。
You're returning false
in the anonymous callback function not in the emailValid
function. Just declare a global boolean variable and alter it accordingly in the anonymous callback function.
这篇关于问题使用XMLHtt prequest后返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!