I am trying to validate if an email entered is present in the database or not and based on this result I execute a code. For this I am calling a Javascript function which calls ajax jquery method to a php page which checks id the email entered is present in database. The code used is :
function validateEmail()
{ var username= document.getElementByName("email").value;
alert("Username is"+ username);
$.ajax({
type: "POST",
url: "emailCheck1.php",
data: "username="+username,
success: function(result){
if(result=="checked"){
return true;
}
else
{
document.getElementById("errorEmail").style.display="inline";
return false;
} }
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
But the problem is the function returns the value before ajax method completes. So the return value is not correct. How can i make the function not return any value until ajax method completes and based on the return value of ajax method i return the required value... Any help will be highly appreciated...