Ajax respones w/ PHP.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Ajax respones w/ PHP.

Post by JellyFish »

Is it true that every time I echo something in my PHP file, that's requested by javascript, this would end up in the responesText of the XMLHttpRequest object?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The output of a PHP script is the response to a HTTP Request to it -- so yes.
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

What all is considered output in this context?


Another thing is, could I start a session and set some session variables, with the requested php? For example, lets say my http request object requests a php page. That php page takes two values from the http request object (username and password). Then checks if the two values match specific values in the database and if so starts a session (i.e php session start function) and sets two session variables to the two values pass by the XMLHttpRequest object (username and password), therefor having the effect of logging in.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

anything that is sent to the client is output. If all you do is check request vars against a database, and then start a session, no output is sent. If your php is configured to use cookies, (which it is by default), your code will send headers to set the cookie on the user's browser, but there is no body in the response.

IE:

Code: Select all

<?php
session_start();
if (isset($_GET['username']))
{
    logUserIn($_GET['username'], $_GET['password']);
}
// no output has been sent yet...

// so here's some output
echo 'output';
?>
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I see. So the response is what the requested file outputs (whether it be xml, html or php). And in the case of using a php file as a request all the variables, cookies and database interactions take place just not responded! Am I wrong in anyway about this?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

PHP files aren't really output since they are executed on the server. PHP can output xml, html, plain text, images etc. All ajax does is allow javascript to request output from php (or any other server-side technology) as opposed to your browser doing it via a form submission or you doing it by directly typing the uri into the url. (ie: http://www.yoursite.com/event.php?action=view&id=23)

I don't know what this means:
all the variables, cookies and database interactions take place just not responded
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

It means like you said, all server-side scripts are executed just not outputed(responded whatever).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

actually after reading my own statement, to be more accurate, ajax doesn't even need to request output. It just needs to request. There is no rule stating that whatever you are requesting has to return output. Sometimes it's just deleting a record in the database or something, but generally even in this case, your server-side code would output a success message or something.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Yeah.
Post Reply