It can be quite intimidating for any new web developer when it comes to developing that first, vital, Ajax (Asynchronous Javascript and XML) application; but it needn't be - not if the web developer remembers that there are just 5 simple steps to building any Ajax application:
- create the XML HTML Request Object
- define the user input and output
- process the user input
- send the processed data to the server
- process the server response
The application developer can build highly complex pieces of software just by following those simple steps.
1. Create the XML HTML Request Object
The XML (Extensible Markup Language) HTML (Hyper-Text Markup Language) request object is at the core of every Ajax application, and it's this object that enables Javascript to communicate with a server; however, the object is handled slightly differently by each web browser (or to be precise, it's handled one way by Microsoft Explorer and another by all other browsers); therefore, the Javascript code needs to check the browser type and create the object appropriately:
2. Define the User Input and Output
As well as communicating with the server the application must also allow the user to input data - and then to see the results (if there are any). This can be achieved by using:
- a simple form for the user input
- div areas for displaying any outputs
and, of course, this is all done by using HTML:
This simple form does not directly call any other web pages or cgi scripts; instead it contains an input button that calls a Javascript function - process_data.
3. Process the User Input
Before sending the data to the server it should be processed for two reasons:
- to ensure that the data has been entered correctly
- to format the data so that it is ready for the server
In this example the code ensures that the user has entered both a first name and a surname, and then displays the result in one of the div areas:
4. Send the Processed Data to the Server
The formatted data can now be sent to the server, but before that can be done the server file must exist - in this case a PHP file:
If this file is saved on the web server as process_user_data.php then it can be called from the Ajax application:
The application now:
- sends the formatted data to the server
- waits for the server to carry out its tasks (denoted by the readyState being set to 4)
- displays the result
5. Process the Server Response
If the data returned is simple (i.e. a single piece of information) then is can be used immediately, but if it's more complex (containing a number of pieces of information) then some more Javascript processing will be needed before the data can be used:
In this example the server response is split into a Javascript array, and then each element of that array is displayed in a different div area.
Conclusion
An Ajax application may appear complicated at first glance - but they're not; they all need to carry out those same 5 simple steps:
- use Javascript to initialise the XML HTML request object
- use HTML to create areas for user input and output
- use Javascript to send the user inputs to a server
- process the data on a server
- use Javascript to process any response from the server and display the result