More on XMLHttpRequest

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:

  1. Create the form
  2. Add the event handler
  3. Create the XMLHttpRequest Object
  4. Talk to the server using an HTTP GET
  5. Build the zip-code database
  6. Build a PHP handler to respond to the GET request and return the right values from the DB.
  7. 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.

Please leave comments using the sidebar.

Last modified: Thu Oct 10 10:47:19 2019.