Advanced Search

Results 1 to 3 of 3

Thread: Ajax callback function

  1. #1
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Ajax callback function

    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.

    Code:
    if ($password == $registered['password']){
    	session_start();
    	$_SESSION["userName"] = $registered['userName'];
    	$_SESSION["sessionId"] = session_id();
    	echo ('Login Successful');
    } else {
    	echo ('Login Failure');
    } // end if
    Code:
    function callback(ajaxResponse){
    	if (ajaxResponse == "Login Successful"){
    		window.location = "home.html";
    	} else {
    		alert('Login Error');
    	} // end if
    } // end callback

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by dan0 View Post
    Code:
    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) )

  3. #3
    Join Date
    Dec 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •