I have a login form,when the user clicks the submit button,I have a main function which i call to decide whether the page should migrate. Inside the main function,I call two other functions:one to check that neither username nor password field is empty and the second function has an ajax that checks if the combination of username and the password is correct: the problem is that the function with ajax doesnt work correctly since the main function doesn't wait for that inner function with ajax to complete(asynchronous): here is the sample code:
<h3 id="myerror" style="display:none;">Invalid username or password</h3>
<form action="mylogin.php" onSubmit="return mymain()">
username<input type="text" id="myuname">
password:<input type="password" id="mypass">
</form>
//and the script
<script>
function mymain() {
if (is_not_empty() && is_in_database()) {
return true;
} else {
return false;
}
}
function is_not_empty() {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
if (uname == "" && pass == "") {
$("#myerror").css("display", "block");
return false;
} else {
return true;
}
}
var a;
function isthere() {
var uname = $("#myuname").val();
var pass = $("#mypass").val();
//the ajax php below returns either isthere or notthere with reference to the username
and password combination.
$.post("http://localhost/passtester/pass1.php", {
username: uname,
password: pass
}, function (feedbak) {
a = feedbak;
});
if (a == "isthere") return true;
if (a == "notthere") return false;
}
</script>
I will really appreciate your help