PDA

View Full Version : Ajax callback function



dan0
12-01-2008, 07:56 PM
My ajax callback function is supposed to determine if the submitted argument is 'Login Successful' and if not perform an alert. However, regardless if the submitted arguement is 'Login Successful' or not the callback function displays an alert.

I have php code that upon verification of the correct email and password combination, echos 'Login Sucessful' otherwise echoes 'Login Failure'. I know my php code is correct, because when I change the callback function to simply alert the responseText either Login Successful or Login Failure is shown depending on whether I submit the correct email/password combo in the login form.

Below are my php and js code. Can someone take a look and tell me where my code is wrong? Thanks.



if ($password == $registered['password']){
session_start();
$_SESSION["userName"] = $registered['userName'];
$_SESSION["sessionId"] = session_id();
echo ('Login Successful');
} else {
echo ('Login Failure');
} // end if




function callback(ajaxResponse){
if (ajaxResponse == "Login Successful"){
window.location = "home.html";
} else {
alert('Login Error');
} // end if
} // end callback

clueful
12-01-2008, 08:18 PM
function callback(ajaxResponse){
if (ajaxResponse == "Login Successful"){
window.location = "home.html";
} else {
alert('Login Error');
} // end if
} // end callback

What do you get if you alert ajaxResponse? There may be some whitespace or new line in there.
Try: if ( ajaxResponse.match(/login success/i) )

dan0
12-01-2008, 09:10 PM
When I displayed responseText via alert I would get Login Successul or Login Failure depending on whether or not I used a correct email/password combo. I too thought that there maybe some extra spaces before or after 'Login Successful' and 'Login Failure' so I adjusted the php code to use trim.



echo trim('Login Successful');


The trim function didn't seem to resolve the problem. However, when I tried your suggestion the code responded as desired. I don't understand how, but I guess there was a new line(s) somewhere in the echoed string. Thanks for the help.