Ajax Basics

Introduction

This page covers the basics of setting up a web page to use Ajax. Ajax stands for Asynchronous JavaScript with XML and really refers to a style of web page development. Instead of the standard request and response cycle of a regular web page, requests are made using JavaScript in the browser. The script then handles making the request for data and formatting the response.

The examples covered here are a summary of the techniques covered in two very excellent books. Please refer to them if you need more information on the subject.

Bulletproof Ajax PPK on JavaScript

Getting a Text File with Ajax

For the first example, a simple text file is retrieved using Ajax.

AjaxHello.html

A quick review of the important parts of the example is in order.

That covers the hightlights.

Retrieving a Date with Ajax

Why don't not try something a little more useful? The following example extends the previous example a little bit. Instead of reading from a file, a PHP script is called with returns the current date and time. This can be called and inserted in the page. Each time the user clicks on the link, the date is updated on the page without any refresh.

AjaxDate.html

Note much has changed from the first example. Line 33 calls an actual PHP script instead of a text file. The PHP script generates the date.

Here is the source code for the PHP script used to generate the date.

getdate-text.php

That completes this quick overview of an Ajax page. See the books linked above for more information on the subject.