A simple AJAX example

1. Introduction

AJAX is a method of allowing your browser to communicate with a host in a way that does not involve completely re-drawing the page you are looking at; instead, it gives the browser the opportunity, if it wants, to modify some part of the page as appropriate. Broadly speaking the sequence of events is:

  1. Your browser makes an AJAX request to a host, asking it to run a particular script, and sending some data for the script.
  2. The script on that host receives the data and uses it to generate some results to return to your browser.
  3. Your browser uses those results to decide how to update your page.

We'll take a simple example of loading up a drop down list. Our list will be empty until you click a button, after which the list will be loaded with items obtained via AJAX. If you then make a selection from the list, an alert showing your selected item will appear.

In our example, we'll see how to build up a string containing several data items that are passed via AJAX to a PHP script, and how those data items are received in the script. We'll also see how a response string with the return data is built up in the PHP script, and how the individual data items are then extracted using JavaScript.

You can try the example here in a new window.

2. The AJAX Request

A webpage makes an AJAX request via JavaScript as a result of taking a number of separate steps. I have encapsulated these in a function that it may be convenient to show now, along with the example of its use.


<meta http-equiv="content-type" content="text/html; charset=utf-8">

<script type="text/javascript">

var  delim0 = String.fromCharCode(0);                       // Create three delimiters
var  delim1 = String.fromCharCode(1);
var  delim2 = String.fromCharCode(2);

var  tmarg  = 5;                                            // Decimal number
var  string = "This is select item number: ";               // String

function ajax (url, data, callbackFunction)

     // Function handles the AJAX calls and data return

     var request = window.XMLHttpRequest ? new XMLHttpRequest () : new ActiveXObject ("MSXML2.XMLHTTP.3.0");

     request.open ("post", url, true);
     request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 
     request.onreadystatechange = function ()

          if  (request.readyState!=4)  return;

          if  (request.status==200)

               if  (request.responseText!="")  callbackFunction (request.responseText);

               request = null;



     request.send (data);


function initialise ()

     // Initialisation for the page, if any, can go here


function loadContinue (results)

     var  i, j, items, subItems, subsubItems, ptr, num, newoption;

     ptr = document.getElementById ("myselect");

     items  = results.split (delim0);                       // Cleave the major data items

     for  (i=0;  i<items.length;  i++)                      // Treat each major item

          subItems = items[i].split (delim1);               // Cleave each item to obtain its data

          switch  (subItems[0])                             // Handle each separate set of data

               case "M":
                    alert (subItems[1]);                    // Don't need to save the message

               case "N":                                    // Just the one data item here
                    num = subItems[1];                      // Save the number of select items

               case "S":                                    // Here is where the select data is treated
                    while  (ptr.childNodes.length>0)  ptr.removeChild (ptr.lastChild);
                    subsubItems = subItems[1].split (delim2);
                    for  (j=0;  j<num;  j++)
                         newOption             = document.createElement ('option');
                         newOption.value       = j;
                         newOption.textContent = subsubItems[j];
                         ptr.appendChild (newOption);

               case "Z":                                    // Just a placeholder

                    alert ("loadContinue: " + subItems);




function loadSelect ()

     // Called when user click the load button

     var  mydata;

     mydata = "num=" + tmarg + "&string=" + encodeURIComponent(string);

     ajax ("ajax-test.php", mydata, loadContinue);


function selectAlert (index)

     // Called when user makes a new selection

     var ptr, str;

     ptr = document.forms[0].myselect.options[index];
     str = ptr.textContent + " (" + ptr.value + ")";

     alert (str);




<body onload="initialise();">

<input type="button" onclick="loadSelect();" value="Click to load the select">
<select id="myselect" onchange="selectAlert(this.selectedIndex);"></select>


You may want to resize your browser window and scroll so that you can see the whole of the function ajax (), which has three arguments. Let's go through that next. The first thing it does is to create an XMLHttpRequest object (q.v.). This object will manage the AJAX transfer, once told to do so via its send method. Our instance of the object will be called request.

If you're wondering about the ?: syntax on that line, we're having to test whether your browser can create an XMLHttpRequest object in the way most browsers can. Some versions of Explorer can't, so for that we need to do something different. The ?: syntax is called a ternary conditional operator (q.v.).

Next, we execute the open method on the request object. There are three arguments to the open method. The first indicates which request type is to be used. I use "post" so that the data to be sent can be encoded separately from the URL (we'll see how that works in a minute). The second argument to open is the URL of the script to be run by the server; this URL has been passed in as the first argument to the ajax function. The third argument to open indicates that you want the request to be sent asynchronously, that is, the ajax function should return as soon as the request is sent, and should not wait until all the data is returned. I set this to true because I often want to have more than one request going on at once. After the call to the open method, these three parameters are stored in the request object, ready for use later

After the open comes a call to the setRequestHeader method (q.v.). The form of this call appears to be mandatory when using the "post" request type.

Skipping forward for the moment to the end of the ajax function, we see a call to the send method. Unsurprisingly, this is where the request is actually initiated and data sent to the remote script. Note that the data being sent has been passed into the ajax function as its second argument.

Now the request has been sent, and soon data will be being returned to your browser. How is this data obtained so it can be made available to the caller of the ajax function? Return now to the middle of the ajax function, where we see the onreadystatechange event handler (q.v.) being set to something, an anonymous function (q.v.). This function will be called each time the readyState of the request object changes. You can look up the various possible values, but the only really interesting one is 4, which means that the request is complete. Notice that for other values, the function does nothing.

Once the request is complete, we check whether it succeeded or not. You will observe that status having value 200 is the test here. If so, then the callbackFunction, supplied as the third argument to the ajax function, will be called, with the responseText (the results returned from the remote script) of the request as its argument. That is where you will process what is returned.

We also check in case the returned status is not 200, so that any corrective action or fixup can be performed. In our case, a simple alert suffices.

Finally, having processed the returned results (if any), notice that we set the XMLHttpRequest obect variable, request, to NULL. To understand why this is necessary, read up about JavaScript's inner functions and closures.

3. Using the ajax function

Now that we've hidden away the innards of ajax, we can see how it's used. In our example, there's just the one call, in function loadSelect (). So scroll down to that and take a look. The three arguments for the call are:

  1. The URL of the script to be run - a string: "ajax-test.php". We'll look at that in a minute.
  2. A data string. We'll see how that's made up next.
  3. The name of the callback function, in this case its function loadContinue (...). We'll look at that after examining the PHP script.

The data string consists of "name=value" pairs separated by ampersands (&). In our example, there are two such pairs, as follows:

     mydata = "num=" + tmarg + "&string=" + encodeURIComponent(string);

You can see how the variables: tmarg and string are initialised at the top of the example, and so the resulting string to be passed as data will be:


We've used encodeURIComponent (q.v.) to ensure that the content of the variable string, which may in general contain special characters, does not present a problem. You'll notice, for example, that the spaces in it get replaced by %20. The string to be sent may have many "name=value" pairs, separated by an & character; Ours has just the two.

You can see from this how to pass data to the script. Let's look now to how the script receives it and passes data back. Our example PHP script looks like this:


// An example of how to get the parameters supplied from the other side in an
// AJAX request, how to create a response, and how to send the response back.
// We use the $delimx to structure the data, so it's easy to generate, and easy to
// break apart again in JavaScript. This works quite satisfactorily and effectively.

$num    = $_POST['num'];                     // Parameters passed through
$string = $_POST['string'];

$delim0 = chr (0);                           // Delimiters for returned data
$delim1 = chr (1);
$delim2 = chr (2);

$outstr = 'Z';                               // Start to build up $outstr as the response string

$outstr .= $delim0 . 'N' . $delim1 . $num;   // First real data item to go back - number of items
$outstr .= $delim0 . 'S' . $delim1;          // Second item - this is the start of it

for  ($i=0;  $i<$num;  $i++)                 // You'd probably get these strings from a list
     if  ($i>0)  $outstr .= $delim2;         // Need delimiter but not for first one
     $outstr .= $string . $i;                // Build up the set of subitems for the second item

$outstr .= $delim0 . 'M' . $delim1 . 'Loading of the select is complete';       // A message the other side can display

echo $outstr;                                // Send the response string back


Notice how the two names in the data string being sent to the script appear as members of the $_POST array in the PHP script, where they may be readily accessed. Notice also that there is an echo statement at the end of the script. This is how data is returned - as a string made up of all the data output by one or more echo statements. So you see that getting data from and returning it to the browser is quite straightforward. It's up to you to decide what data needs to be transmitted back and forth, of course.

The header () function call avoids problems if you are using multi-byte UTF-8 characters.

4. How to structure the data

So far, we've seen the outlines of how to make an AJAX request and receive returned data. You will note that the data - all of it - is in the form of a string. When I started using AJAX, the first thing I did was think about how to pass structured data back, such as an error messages and an array of numbers. I kept reading that XML was what got passed back, and that may certainly be used for structured data. However, I couldn't see where the XML was generated or decoded, and after a while I realised that it was up to me to arrange the encoding and decoding.

That all seemed unnecessarily complicated for my purposes, so I designed a simple protocol wherein data is delimited. Given that all that one is allowed to pass back is a string, I chose as delimiters characters that would not form part of the data. It would then be my responsibility to ensure that these characters never did, in fact, appear in the data stream. The characters I chose were:

These have values 0, 1, and 2 respectively and are the first three characters in the 7-bit ASCII table. In PHP they may be conveniently generated by the chr() function, as seen in the PHP script above. In JavaScript, String.fromCharCode() does much the same thing. The way these are used is as follows.

delim0 is used as a major separator between data items. In our example, there are three data items, each of which is tagged with a letter. delim1 is then used to separate each piece of data within that data item. The major data items are sorted and treated in loadContinue. It's a simple matter to cleave the data at the major item boundaries using string.split(), (rather as DNA is cleaved by enzymes in the cell), and then treat each piece separately. loadContinue is set up to handle that in a clear way.

In the case of the "N" and "M" major items, we see that each has one data item. For "N", this is a number, and for "M" it is a string. The "S" major item has just one subitem, which is a set of strings. The strings in that set are delimited by the third of our delimiters, delim2. An extra level of cleaving is required in loadContinue to access these.

5. Points to note

5.1 Those letters

I always put a single letter as the first thing in a major data item, so that it's easily visible when doing the decoding in JavaScript, and can be easily spotted in the switch statement. In the application where I'm using this technique, there are about 100 separate calls to the ajax function. The letters get reused as necessary, but I try to use a letter consistently. Thus, "L" is always used to indicate that the particular PHP script wants to output a log message for the user.

5.2 loadContinue

Personally, I always call these callback-functions xxxxxxxContinue just to emphasise that they are callback functions. I also always structure them this way, even if there is only one item of data to return. For one thing, that may get expanded later, for another it allows for the case where an unexpected item is returned; this latter is then highlighted by the alert(). Usually this has meant an unexpected situation has arisen. This can then be coded for and the problem eliminated.

I also try to keep the callback functions compact, with any large amount of code for a data item relegated to another function. This is illustrated here by the getColour function, which is obviously small in our example. This is also when the next level of delimiter, delim2 comes into play.

5.3 What is the "Z" item for?

It's a convenience item. Sometimes one is building the response data in a loop, and you'd like to put out the separator first. In our example it's not really required, but in a more complex case, this is a simple way to avoid bugs from having one too many separators. As it's low-cost in terms of processing, I routinely add it in all cases.

5.4 Do I need to do all this?

Not if you have a really simple case or use for AJAX where you just want to get a single piece of information from a script back to the JavaScript. I developed this approach because I knew at the outset that my application was going to become quite large, and I'd have a variety of data to return.

6. Finally ...

You may be a novice programmer, or an experienced programmer who happens to be new to PHP, JavaScript, or HTML. Either way, I'd recommend working through this example.