Bill Bercik has a nifty little tutorial on using XMLHttpRequest. His purpose is "to demonstrate through a series of baby steps just how easy it is to use the XMLHttpRequest object." The example is a simple form that fills in, using XMLHttpRequest, the city and state after you type the zip code. He does it in seven steps:
- Create the form
- Add the event handler
- Create the XMLHttpRequest Object
- Talk to the server using an HTTP GET
- Build the zip-code database
- Build a PHP handler to respond to the GET request and return the right values from the DB.
- Clean up the code to handle exceptions
There's no need to use XML in this simple example, but he shows how to do it anyway, since more complex examples will need it.
The trick to XMLHttpRequest is that it has a method, onreadystatechange that lets you register a function to handle the return request. So, the event handler fires off the HTTP request and then the code sits back and waits for the response. Once you know how to create the XMLHttpRequest object (and that's mostly cut and paste) and see how to use it, the rest is pretty straightforward.