AJAX beginner’s tutorial

AJAX stands for Asynchronous JavaScript and XML and is used for allowing the client side of an application to communicate with the server side of an application.

This might be necessary in order to update the contents of a drop down menu, or to check the availability of a user name, all without reloading the whole page.

Using AJAX isn’t very hard and in this tutorial I’ll show you how to get up and running.

Bring on the jQuery

Although not strictly necessary, I always recommend using the jQuery library when dealing with AJAX. It simplifies the syntax considerably and covers various edge cases behind the scenes.

That said, let’s begin. First of all, we’ll need a simple HTML page which will submit an AJAX request to a PHP script when a button is pressed.

As you can see, when the button is clicked, the contents of the text input are read into the variable called text.

After that a POST request is sent to submit.php. This request will pass the contents of the text variable to the PHP script. We also tell the browser not to cache the response.

You might take a minute to notice that the data attribute accepts an object literal (denoted by the {} syntax). This object literal can contain as many key/value pairs as desired.

The success attribute accepts a function which will be executed if the AJAX request completes successfully. Inside this function, the variable response will contain the server’s response.

The error attribute also accepts a function which will be executed when the AJAX request has completed. This is useful for debugging purposes.

On to the PHP

In our PHP script, we will extract the variable submitted via the form and simply return it to the JavaScript:

$value = $_POST["value"];
echo "I received '" . $value . "'";

The JavaScript will have access to this within the success callback and can alert it to the user.

In the real world, you would use this value to extract data from a database, or something similar, but for the purposes of this tutorial, I’m going to leave it here.

And that’s how AJAX works. Simple.

You can try out our little demo for yourself here.

I hope this proved useful for people. If you have any questions, I’d be glad to hear them in the comments.

If you made it this far then you might consider following me on Twitter:

This post currently has 2 responses

  1. Alfonso SANCHEZ says:

    Very good material, it can be undestood and used quicky.

Comments are closed!

I'm on Twitter: