...

View Full Version : Having a really hard understanding jQuery ajax()



skcin7
03-27-2011, 02:41 AM
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:


<!--

$(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


<?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.

oesxyl
03-27-2011, 02:53 AM
try this:


<?php
header("HTTP/1.0 404 Not Found");
?>


best regards

skcin7
03-27-2011, 02:57 AM
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. :-)

oesxyl
03-27-2011, 03:04 AM
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

skcin7
03-27-2011, 03:08 AM
Many thanks for your quick but EXTREMELY helpful tip. :)

oesxyl
03-27-2011, 03:13 AM
Many thanks for your quick but EXTREMELY helpful tip. :)
you are welcome, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum