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.