Understanding AJAX

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am writing a PHP app and am trying to utilize AJAX with this.  When
I use AJAX to call a PHP script, how do I tell the PHP script what I
want from it?  Is there another way to do this other than either
having a different PHP script for every action or passing in a text
querystring to the PHP script and then doing some kind of switch
statement to figure it out?

Thanks for explaining this.  Also, it seems like the only time it
makes sense to use the AJAX is if I want content to change without
refreshing the page, so something like having the date in the upper
corner of the page might as well be generated by PHP instead of
calling a PHP script with AJAX, right?

Thanks for your help.


Re: Understanding AJAX

KDawg44 wrote:
Quoted text here. Click to load it

The PHP script that responds to the Ajax call can function in the same
way as any PHP script - you need SOME way to tell it what you want done,
so you're going to have to either pass parameters in, or call a
different script. Is there any other way to get a PHP page to know what
you want to do without having a different script or passing in a query

Ah - yes, there is: You can submit data via POST. This is also possible
with Ajax, but it is somewhat more complicated, and rarely worthwhile, IMO.

Quoted text here. Click to load it

That would seem to be the main point of Ajax :) Seems like you get it
pretty well.

Re: Understanding AJAX

Quoted text here. Click to load it

Thanks for the response!  Always good when I get it...  :)


Re: Understanding AJAX

KDawg44 wrote:
Quoted text here. Click to load it

One thing to bear in mind - which can be useful - is that cookies and
such are passed through on the Ajax call as well (for the most part,
it's not much different than a regular HTTP request), so you can use
session variables in the Ajax script if you need.

Re: Understanding AJAX

KDawg44 wrote:

Quoted text here. Click to load it

When you visit a .php page with a web browser, how do you tell what do you
want from it?

- GET variables in the URL, like in "foobar.php?foo=foo&bar=baaz"
- POST variables (like a form), via HTTP
- Some other complicated mechanisms (cookies, custom headers, source IP)

So, now, guess how can you tell a .php script called from ajax (instead of
from a web browser) what fo you want from it.

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Now listening to: Paco Fernández - Café del Mar, volumen doce (2005) - [5]
Junto al mar (6:39) (95.500000%)

What do you really need to know about AJAX

1. First of all, XMLHttpRequest have idiotic API. onreadystatechange
handler have no parameters! (Thanks to Microsoft) So if you have more
then one request on same page for different resources you need to have
separate handler for every request because event handler have no way to
determine which exactly object calls it.
2. It's impossible to have separate handler for every request if you
have variable amount of resources to request (for example tree with
dynamically requested branches). Thanks God javascript was developed by
wise programmers so in normal browsers this problem is solved using
javascript closures.
3. But in IE this is impossible due memory leak. IE garbage collector
can't remove circular references, and this problem is not solved even in
IE7. (Thanks Microsoft again)
4. So we have to make separate component called like 'AjaxChannel'
implementing queue of requests and other components must transfer data
throw it's API.
5. IE don't support readyState==3 so you can't use technique of endless
connection for server-generated events. You need to close down
connection after every server event and reopen it again after processing
data because in IE data will be available ONLY on readyState==4. One
more thank to Microsoft.
6. IE don't support national encodings in XMLHttpRequest so if you have
   non-english site you have to use UTF-8 only. If site MUST BE in any
national encoding (like cp-1251 as in my case) you have to implement
recoding routines in javascript and PHP to convert your_site_encoding
<-> UTF-8. BIG thanks to Microsoft.
7. After all I recommend to make additional shutdown like this
(specially for IE again):

<script type="text/javascript" for="window" event="onunload">
// Hack for IE

downAjax is the XMLHttpRequest object. Do it for every XMLHttpRequest
object used.

Have nice ajax! :)

Re: Understanding AJAX

On Tue, 05 Feb 2008 17:33:16 -0800, KDawg44 wrote:

Quoted text here. Click to load it

you mean you want USE ajax. you just pass a query string, like

adiv.innerhtml = ajaxrequest('script.php?param1=this&param2=that');

Re: Understanding AJAX

Quoted text here. Click to load it

I would suggest taking the headache our of AJAX by using PROTOTYPE.
Below is a PHP example of this. As you can see, AJAX is invoked in a
single line 'new Ajax.Updater'.

It can also pass parameters to the PHP script, in this case as
parameter called 'task' that has the value 'details', and is picked up
by the PHP script :-

if (@$_POST['task'] =3D "details") {
        echo "This is some text returned from the PHP engine via



<title>Ajax Test</title>
<script src=3D"prototype.js"></script>


function update_content() {
        new Ajax.Updater('dynamic', 'ajaxtest.php', { method: 'post',
parameters: { task: 'details' } });


<table border=3D"1" width=3D"100%">
    <td width=3D"33%" onclick=3D"update_content()"><a href=3D"#">Update</a><=
    <td width=3D"33%">&nbsp;</td>
    <td width=3D"34%">&nbsp;</td>
    <td width=3D"33%">&nbsp;</td>
    <td width=3D"33%"><div id=3D"dynamic">I am going to put some content
    <td width=3D"34%">&nbsp;</td>
    <td width=3D"33%">&nbsp;</td>
    <td width=3D"33%">&nbsp;</td>
    <td width=3D"34%">&nbsp;</td>



Re: Understanding AJAX

Rob wrote:
Quoted text here. Click to load it

Not much headache to Ajax, really.
And there are much better libraries to use than Prototype, which was
really designed so that server-side programmers don't actually have to
learn javascript. Personally, I never understood why that's such a problem.

http://abcdefu.wordpress.com/2006/12/27/prototypejs /

Try jQuery or Yahoo if you really need a library.

Site Timeline