[SOLVED] how to "return" true/false from PHP to AJ

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
martinco
Forum Newbie
Posts: 18
Joined: Mon Jun 25, 2007 10:06 pm
Location: Costa Rica

[SOLVED] how to "return" true/false from PHP to AJ

Post by martinco »

hi!

i made a ajax script that needs to know if it has to reload the information shown on the page.

i wrote a php script that compares an id with the last id on a db table,... but how can i "return" a true/false expresion :?: and how can ajax catch that :?:

thanks!
"Pura Vida!"
Last edited by martinco on Tue Jul 03, 2007 7:16 pm, edited 1 time in total.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

The only way to return true within a response from php is to have php echo something like:

Code: Select all

exit("true");
And for false:

Code: Select all

exit("false");
And in you Ajax callback:

Code: Select all

if (data = "true")
{
// do something
}
else
{
// do something else
}
User avatar
martinco
Forum Newbie
Posts: 18
Joined: Mon Jun 25, 2007 10:06 pm
Location: Costa Rica

Post by martinco »

that's new for me... almost everything about php is new for me...

i did the following in my php script:

Code: Select all

echo "true"
and in ajax:

Code: Select all

if(ajax.responseText.indexOf("true") != -1) {
...
} else {

...
}
maybe it is better using exit(), i'll try.

thanks and "Pura Vida!"
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Could we see some of your PHP script?
User avatar
martinco
Forum Newbie
Posts: 18
Joined: Mon Jun 25, 2007 10:06 pm
Location: Costa Rica

Post by martinco »

sorry, i was out all the weakend...

what part of the php code do you want to see? Here's what i do to "return" a boolean value from PHP:

Code: Select all

...
$row=mysql_fetch_array($result);
if($row["id"] > $lastid) {
	echo 'true';
} else {
	echo 'false';
}
...
And here's the other (AJAX) side which makes the request and gets the boolean value:

Code: Select all

...
if (ajax.readyState==4) {
	if(ajax.responseText.indexOf("true") != -1) {
		if(debug) alert("Update? Yes");
		getLastId();
		loadMessages(div_id);
	} else if(debug) alert("Update? No");
	setTimeout('update("'+div_id+'")',speed);
}
...
what does exit() do? and i don't understand the javascript line if (data = "true") JellyFish posted.

"Pura Vida!"
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

exit() pretty much exits the current script.

What I meant by (data = "true"), and I really meant (data == "true"), was that data be the responseText of the Ajax request. Anyways you did better utilizing the indexOf() method rather then a straight comparison(which is what I suggested).

You might want to use the exit() function rather then echo, but it really depends on you php script.

What exactly are you having trouble with?
User avatar
martinco
Forum Newbie
Posts: 18
Joined: Mon Jun 25, 2007 10:06 pm
Location: Costa Rica

Post by martinco »

thanx jellyfish!
i actually have no problem. my script is running fine and i got what i wanted with the script i posted.
a read about exit() but in this case i'll use echo because after this i have to close a mysql connection.
indexOf and charAt are just two great js functions, just keep that in mind, and sometimes you'll find great usages for them
Post Reply