NameSilo

Help please, small AJAX request.

Spaceship Spaceship
Watch

LogiK

Established Member
Impact
11
Hello,
Recently I've been reading the book "AJAX for Dummies".. Its a good book and all, but it doesn't show you much about AJAX & PHP, The reason I want to learn AJAX is because I want to accomplish something like this on my website:
Click here to view image..

I would like to know if this is possible, I am familiar now with simple AJAX and external files, but I would like to know more about using it with PHP, The tutorials I've searched for on the Internet don't put it quite clearly enough. So maybe a NPer can give me an example? or possibly link me to a program that does the AJAX for you like I've seen around, until I fully understand using AJAX & PHP together.

Thanks for reading,
All help will really be appreciated and +rep too.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
That's mainly dealing with status codes for the loading part. Check out http://developer.mozilla.org/en/docs/AJAX:Getting_Started and scroll to the bottom to the final code.

Code:
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
You could make an } else { after this to have it change the HTML to a loading image or something.

Do you need help with the PHP?
 
1
•••
Thanks, Dan.
No I'm all good on the PHP front.
 
0
•••
The important thing to realize is that as far as the AJAX code is concerned, the PHP part doesn't matter!

What I mean is that the AJAX code doesn't care what the page it's calling is written in. It's asking for a web page just like any browser would ask for it.

Likewise, the page being called doesn't know (reliably) who the caller is: a browser, an AJAX library, a CURL script, the wget program, a raw socket, whatever.

That said, if the PHP script you're calling can at least assume that it's being called from AJAX, it can make certain assumptions. For example, it could perform whatever actions are requested of it (update a database, retrieve information, whatever), and then return a status code as the "content". No doctype, no html, just a status code.

A php file which does this could be as simple as

PHP:
<?php
     // do something 
     $status = your_function_here();
    echo $status;
?>

If you want to get fancy, you could send back a more detailed response, perhaps a pipe-delimited string:
PHP:
<?
     // do something 
     list($status,$message) = your_function_here();
    echo $status, '|', $message;
?>

In your AJAX code, you would then split the string on the '|' character and do something with the content:
Code:
if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {

	         var parts = httpRequest.responseText.split('|');
		
                // parts[0] is the result code
	  	// parts[1] is the message
                // now you can display them or do whatever	
            }
}

Of course you might need to send back a lot more information. You could
do that with something as simple as "\n"-terminated pipe-delimited strings or something as complicated as JSON-encoded objects.

But what it boils down to is the "handshake" between the target page (the php script) and the caller (the AJAX object). The AJAX object has to know the format of the data returned by the script object, and even more importantly, has to fail gracefully if no data or malformed data is returned to it.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back