Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts

    Having a really hard understanding jQuery ajax()

    I have been having a really hard time getting ajax working with PHP for a web application. I want to use jQuery's ajax() method to asynchronously login a user when he enters correct information into a login form.

    The biggest thing I don't realize is HOW the freak does jQuery decipher the difference between a "success" script and an "error" script. It seems that pretty much anything I put into my PHP page, the success callback always gets called and error never does. Am I doing something wrong?

    This is my jQuery javascript:
    Code:
    <!--
    
    $(document).ready(function(){
    
    	$('#login').click(function(){
    		$.ajax({
    			type: "POST",
    			url: "./ajax/login.php",
    			data: "username=" + $("#username").val() + "&password=" + $("#password").val() + "&public_computer=" + $("#public_computer").val(),
    			success: function(msg)
    			{
    				alert("successful" + msg);
    			},
    			error: function(msg)
    			{
    				alert("unsuccessful" + msg);
    			}
    		});
    		return false;
    	});	
    });
    // -->
    This is my very basic 'login.php' page. Despite the exit and die() and the syntax error, the success() jQuery callback is always called
    Code:
    <?php
    
    exit;
    die();
    header("Location: http://nicholas-morgan.com");
    header('Location: www.example.com');
    
    THIS IS A SYNTAX ERROR
    
    $_SESSION['authenticated']['status'] = true;
    $_SESSION['authenticated']['user_id'] = 1;
    
    ?>
    When I click the "login" button of the login form, it says: successful<br />
    <b>Parse error</b>: syntax error, unexpected T_STRING in <b>C:\xampp\htdocs\senior_project\ajax\login.php</b> on line <b>8</b><br />
    .

    Furthermore, even when I remove the syntax error and the code to kill the script, the SESSION variables that I try to set don't seem to be set for the rest of the application. I need these SESSION variables to be set to keep the user logged in. EDIT: NEVERMIND about this part. I figured out it's because I forgot session_start(). DUH!

    Help GREATLY appreciated. I've been stuck on this for way too long and it is frustrating.
    Last edited by skcin7; 03-27-2011 at 03:49 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    <?php
    header
    ("HTTP/1.0 404 Not Found");
    ?>
    best regards

  • Users who have thanked oesxyl for this post:

    skcin7 (03-27-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    Ha. Cool! That works good and gives an unsuccessful script. Yay. Thank you. Is that the best way to do it? I looked in the jQuery documentation and also searched Google and coudln't find much information about it. Awesome I will play around with this. :-)

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skcin7 View Post
    Ha. Cool! That works good and gives an unsuccessful script. Yay. Thank you. Is that the best way to do it? I looked in the jQuery documentation and also searched Google and coudln't find much information about it. Awesome I will play around with this. :-)
    success or error depend on the http state.
    look to the status and readyState on this page:

    http://www.javascriptkit.com/jsref/ajax.shtml

    when readyState is 4, complete and http state is 200, is success.
    in all other cases can be error or not, depend.
    when a page is loading for example is not error,

    best regards

  • Users who have thanked oesxyl for this post:

    skcin7 (03-27-2011)

  • #5
    Regular Coder
    Join Date
    Jul 2009
    Posts
    186
    Thanks
    72
    Thanked 2 Times in 2 Posts
    Many thanks for your quick but EXTREMELY helpful tip.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by skcin7 View Post
    Many thanks for your quick but EXTREMELY helpful tip.
    you are welcome,

    best regards


  •  

    Posting Permissions

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