4.2 A Simple Ajax Example
We will use form validation and check that a given username is acceptable for use. To create the simplest of
simple examples, we need three files for this example: an HTML page for the form, a JavaScript file to
perform the XMLHTTPRequest and a PHP file to do the server side validation. As the user types each
character into the form field, a message appears underneath it showing any errors in their choice of name.
The application in action is shown in Figure 4.2.
Figure 4.2: Simple Ajax example showing an error message whilst typing into the text field
The flow of information in an Ajax request is a little more complicated than a straight web page request. It all
starts with an HTML element containing a JavaScript callback function, such as ???onclick??? which executes the
JavaScript callback code. The JavaScript callback initiates a request to the web server using the
XMLHttpRequest system and the server side code such as PHP performs the work. Once the PHP code has
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
completed, it formats the response in either XML or JSON and sends it back to the browser where it is
processed by a JavaScript callback.
Pages:
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125