Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2011, 02:41 AM   PM User | #1
skcin7
Regular Coder

 
Join Date: Jul 2009
Posts: 186
Thanks: 72
Thanked 2 Times in 2 Posts
skcin7 is an unknown quantity at this point
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.
__________________
My website: http://www.nicholas-morgan.com/

Last edited by skcin7; 03-27-2011 at 02:49 AM..
skcin7 is offline   Reply With Quote
Old 03-27-2011, 02:53 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
try this:
PHP Code:
<?php
header
("HTTP/1.0 404 Not Found");
?>
best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
skcin7 (03-27-2011)
Old 03-27-2011, 02:57 AM   PM User | #3
skcin7
Regular Coder

 
Join Date: Jul 2009
Posts: 186
Thanks: 72
Thanked 2 Times in 2 Posts
skcin7 is an unknown quantity at this point
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. :-)
__________________
My website: http://www.nicholas-morgan.com/
skcin7 is offline   Reply With Quote
Old 03-27-2011, 03:04 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
skcin7 (03-27-2011)
Old 03-27-2011, 03:08 AM   PM User | #5
skcin7
Regular Coder

 
Join Date: Jul 2009
Posts: 186
Thanks: 72
Thanked 2 Times in 2 Posts
skcin7 is an unknown quantity at this point
Many thanks for your quick but EXTREMELY helpful tip.
__________________
My website: http://www.nicholas-morgan.com/
skcin7 is offline   Reply With Quote
Old 03-27-2011, 03:13 AM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by skcin7 View Post
Many thanks for your quick but EXTREMELY helpful tip.
you are welcome,

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:21 AM.


Advertisement
Log in to turn off these ads.