简体   繁体   中英

I'm trying to run this function to check if the database has the same email

function runthis(){
        var email = $("#email").val();

          var xmlhttp = new XMLHttpRequest(); 
            var url = serverURL() + "/Achecksameemail.php"; 
                url += "?email=" + email;

        xmlhttp.onreadystatechange=function() { 
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
            runthisResult(xmlhttp.responseText);
                 } 
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();   
        }


    function runthisResult(response) {
        var arr = JSON.parse(response);     
        for(var i=0; i < arr.length;i++){

            if(arr[i].email = $("#email").val()){
            alert("This email has already been used. Please use another email.");

        }

        else { 
            savenewuser();
        }
    }

}       

However, when it checks that the email has not been used, it can't run the second function to save the user details to the database. Anyone know why? Thank You!

Check email existing and validation in Achecksameemail.php

$response = [
    'exists' => TRUE,
    'msg'    => ''
];

if(filter_var($_GET['email'], FILTER_VALIDATE_EMAIL))
{
    $select = mysql_query("SELECT `email` FROM `users` WHERE `email` = '".$_GET['email']."'");

    if(mysql_num_rows($select))
        $response['msg'] = 'This email is already being used';
    else
        $response['exists'] = FALSE; 
}
else
{
    $response['msg'] = 'Invalid email address!';
} 

echo json_encode($response);

And response in js

function runthisResult(response)
{
    response = JSON.parse(response);     

    if(response.exists)
        alert(response.msg);
    else
        savenewuser();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM