Java Reference
In-Depth Information
Note that the Password and Verify Password fi elds are just for show in this example. Verifying a
password is certainly something the server application can do; however, it is far more effi cient to let
JavaScript perform that verifi cation. Doing so adds more complexity to this example, and I want to keep
this as simple as possible to help you get a grasp of using Ajax.
Next to the Username and Email fi elds will be a hyperlink that calls a JavaScript function to query the
server with the HttpRequest module you built earlier in this chapter.
As mentioned earlier, Ajax is communication between the browser and server. So this example needs a
simple server application to validate the form fi elds. PHP programming is beyond the scope of this topic.
However, I should discuss how to request data from the PHP application, as well as look at the response
the application sends back to JavaScript.
Requesting Information
The PHP application looks for one of two arguments in the query string: username and email.
To check the availability of a user name, use the username argument. The URL to do this looks like the
following:
http://localhost/formvalidator.php?username=[usernameToSearchFor]
When searching for a user name, replace [usernameToSearchFor] with the actual name.
Searching for an e-mail follows the same pattern. The e-mail URL looks like this:
http://localhost/formvalidator.php?email=[emailToSearchFor]
The Received Data
A successful request will result in one of two values:
available
— Means that the user name and/or e-mail is available for use.
available — Signifi es that the user name and/or e-mail is in use and therefore not
available.
not
These values are sent to the client in plain text format. A simple comparison will enable you to tell the
user whether their name or e-mail is already in use.
Before You Begin
This is a live-code Ajax example; therefore, your computer must meet a few requirements if you wish to
run this example.
A Web Server
First, you need a web server. If you are using Windows 2000 (Server or Professional), Windows XP
Professional, Windows Server 2003, Windows Vista Business or higher, or Windows Server 2008, you
Search WWH ::




Custom Search