AJAX — or Asynchronous JavaScript and XML is the newest buzz word in web development. Utilizing a client-side XMLHTTPRequest object, HTML, JavaScript and CSS, you can replace the page refresh that has (up until a few months ago) been a mandatory part of posting data back to a web server.
In this article I’m going to show you 3 simple and practical uses for AJAX on your web site. You’ll need to know a bit of HTML, JavaScript and PHP to follow along, but I’ll do my best to explain everything in simple terms so you don’t get lost!
Note: In this article I will assume that you’re running a PHP web server.
Enabling Client-to-Server Communication
There are 2 simple functions that are required for AJAX to work. They handle everything needed to pass data to the server and also to process the response back from the server. We will create a .JS file containing these 2 functions. They will be used in each of our 3 AJAX examples.
Create a new file in your favorite text editor and call it ajax.js — copy and paste the following code into the file and save it:
function DoCallback(data)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open(‘POST’, url, true);
req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
req.send(data);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject(‘Microsoft.XMLHTTP’)
if (req) {
req.onreadystatechange = processReqChange;
req.open(‘POST’, url, true);
req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
req.send(data);
}
}
}
function processReqChange() {
// only if req shows ‘loaded’
if (req.readyState == 4) {
// only if ‘OK’
if (req.status == 200) {
eval(what);
} else {
alert(‘There was a problem retrieving the XML data: ‘ +
req.responseText);
}
}
}
Now that you’ve created your ajax.js file, let’s move onto our first use for AJAX on your web site.
Instead of posting back to the server to validate a username/password for a login form, let’s see how you can do it using AJAX.
Let’s start by creating the PHP script that AJAX will

I have already implemented the Login script in ajaxa last week. I have my login script to work with a LDAP Server. this has to be best feature that has been added to our office intranet given so many issue proxy servers and so on. on top of that it also lot more user friendly.
Great job Mitchell, your the man…
A very good script espeially for php programmers.
Thanx a lot.
Best tutorial, very clear and easy. Thanks for you work (For Spanish Users: Muy buen tutorial, bastante facil de entender y realmente funciona al implementarlo)
good example good code!
THIS IS A GREAT FEATURE
Very nice explanation and examples. I had no idea it could be so easy.
Good Tutorial… Thanks.
what does eval(what) do ?
Hey, this tutorial rocks!
i've been searching for houres, and this is the best ajax tutorial with example! congrats dude!
Very good tutorial, thank you!
Really good work, thanks.
great tutorial, helped me a lot. thanks.