mercredi 1 juillet 2015

(NATIVE JAVASCRIPT) - The function with ajax request does not provide a response

I'm currently working in a module where in the user will going to register and the following methods that I'm going to mention will check if the email of the registrant is already in use.

Why is it the function with a ajax request does not return any value to other function with ajax request? By the way, I'm not using any javascript framework such as jquery, just a plain and native javascript ;)

By the way, here is the code ^_^

function checkEmailExistense(email){
    var url = "library/functions.php?action=emailchecker&emailadd=" + email;

http.onreadystatechange = function(){
    if (http.status == 200 && (http.readyState === 4)){
        var res = http.responseText;
        if (res == "Invalid") {
            return 0;
        }
    }
}

http.open("GET", url , true);
http.send();

}

On another javascript function wherein I have an ajax request for registration, i have an if statement to check if the returning value of the function above is 0;

var emailaddress = document.getElementById("emailadd").value;

if (checkEmailExistense(emailaddress) == 0){
        errorcount+=1;
        errorstatement+="Email already exist";
    }

I don't have a problem with my php query, but here is my code ;)

$action = $_GET['action'];
switch ($action){
    case 'emailchecker':
        checkTheEmailAdd();
    break;
}

function checkTheEmailAdd(){
    $email = $_GET['emailadd'];

    $connection = new connection();
    $realconnection = $connection->connect();

    $getCount = mysqli_query($realconnection, "SELECT user_email FROM tbl_user WHERE user_email = '".$email."'");


    if(mysqli_num_rows($getCount) > 0){
        echo "Invalid";
    }

}

Looking forward for some answers. Thank you!

Aucun commentaire:

Enregistrer un commentaire