使用XMLHttpRequest后返回false的问题

时间:2010-12-13 23:22:42

标签: javascript ajax javascript-events return-value return-type

我找到了这个Related entry和其他一些但我还没有找到答案,所以我发布了新的。我正在尝试使用我对Ajax的理解从Javascript运行数据库查询。如果电子邮件已经在数据库中,我需要返回false,如果没有,则返回true,但我似乎无法弄清楚我做错了什么。在我开始乱搞并使其变得混乱之前,我已经发布了我的第一次尝试。

javascript:无论如何,它似乎都会返回。

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的东西工作得很好,这也是我困惑的原因。

php:据我所知,工作正常。

<?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 query 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是确认电子邮件。

<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>

谢谢大家的帮助。我希望我已经做好了。我对任何想法持开放态度,因为我对此很新。

3 个答案:

答案 0 :(得分:2)

您将在false函数中的匿名回调函数中返回emailValid。只需声明一个全局布尔变量,并在匿名回调函数中相应地更改它。

答案 1 :(得分:1)

并非emailvalid javascript函数中的所有代码路径都返回一个值。例如,在致电xmlhttp.send(null);后,您没有返回任何内容。

您的大多数return语句实际上都是从onreadystatechange函数返回的{匿名函数返回 not ,我猜测的是不是你想要的。

答案 2 :(得分:0)

与问题无关:我在这里发现了无法访问的代码

    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;
    }

第二个“if”内的代码永远不会被执行,因为电子邮件!=''根据第一个“if”。