I have an if statement:
if(authenticate_session("secret_password12345")){
alert("You are authenticated");
}else{
alert("Intruder alert!");
}
and the javascript authenticate function, which makes an jQuery POST AJAX call to the server to do the actual authentication, and returns true or false:
function authenticate_session(session_id){
$.post("/auth.php",
{ action: "authenticate_session", login_id: session_id },
function(data){
if(data == "denied"){
alert("denied");
return false;
}else if(data == "accepted"){
alert("accepted");
return true;
}
},
"text");
}
However, when the if statement invokes the athentication function, it doesn't wait for the AJAX to complete, and always executes the 'else'. If I add 'return true;' into the first line of the authentication function, before the ajax, the if statement performs as expected => alert("You are authenticated").
I know that the server side is performing properly, as the alert("denied") and alert("accepted") are being called as expected (but interestingly, after the if statement alerts).
How can I make my if statement wait until the authentication function has completed?
Thanks!