php - (NATIVE JAVASCRIPT) - The function with ajax request does not provide a response -
i'm working in module in user going register , following methods i'm going mention check if email of registrant in use.
why function ajax request not return value other function ajax request? way, i'm not using javascript framework such jquery, plain , native javascript ;)
by way, here code ^_^ note: xmlhttprequest() okay. nothing :)
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 javascript function wherein have ajax request registration, have if statement check if returning value of function above 0;
var emailaddress = document.getelementbyid("emailadd").value; if (checkemailexistense(emailaddress) == 0){ errorcount+=1; errorstatement+="email exist"; }
i don't have problem php query, here 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 tbl_user user_email = '".$email."'"); if(mysqli_num_rows($getcount) > 0){ echo "invalid"; } }
looking forward answers. thank you!
ajax calls asynchronous. checking isn't there yet. if wasn't asynchronous, return statement inside onreadstatechange handler has no effect on checkemailexistense
method.
instead use callback invoked request returns data:
function checkemailexistense(email, callback){ 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") { callback(0); } else { callback(1); } } } http.open("get", url , true); http.send(); }
and check:
var emailaddress = document.getelementbyid("emailadd").value; checkemailexistense(emailaddress, function(success) { if(success === 0) { errorcount+=1; errorstatement+="email exist"; } });
Comments
Post a Comment