Page 1 of 1
Ajax respones w/ PHP.
Posted: Tue Jan 30, 2007 9:21 pm
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?
Posted: Tue Jan 30, 2007 9:36 pm
by Christopher
The output of a PHP script is the response to a HTTP Request to it -- so yes.
Posted: Tue Jan 30, 2007 11:34 pm
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.
Posted: Tue Jan 30, 2007 11:50 pm
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';
?>
Posted: Wed Jan 31, 2007 12:01 am
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?
Posted: Wed Jan 31, 2007 12:11 am
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
Posted: Wed Jan 31, 2007 1:22 am
by JellyFish
It means like you said, all server-side scripts are executed just not outputed(responded whatever).
Posted: Wed Jan 31, 2007 1:27 am
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.
Posted: Wed Jan 31, 2007 3:10 pm
by JellyFish
Yeah.