Information Technology Reference
In-Depth Information
If you enter a name that's open, like “skip,” you'll see a message of success, as shown
in Figure 12-17.
Figure 12-17. Sweet victory
If, however, the username you want has already been taken by some awesome person,
then you'll see a sad message such as the one in Figure 12-18.
Figure 12-18. The “not available” screen
Incorporating this technique across an entire web form will allow you to not only check
usernames on the fly, but also validate information so that the user never has to hit a
“submit” button only to find that he or she has put in an invalid number, e-mail address,
or username.
Before we finish, there are two quick tips regarding forms that you might find helpful.
The first is to consider to what event you would like to bind the AJAX checking code. In
this example, we've bound to the onkeyup event on the form field that we're checking
(the username). However, this causes a lookup action on each keypress, something that
might bog down your script. An alternative would be to place the function code in the
next element (e.g., perhaps a password field) in the onfocus event instead of onkeyup .
This will cause the AJAX request to fire after the user has moved to the next field on the
form, reducing load. A second consideration involves validation and auto-submission of
forms. It can be tempting to make a form auto-submit once the last field is completed.
After all, you might be validating information as users enter it, so why make them wait
after they finish the last field. This could be a problem though, as some people may wish
to check their answers before they continue. It might be best to keep a “submit” button
on the page, and let users continue when they are ready.
AJAX Considerations
The examples we've used in this relatively short chapter span the three major types of
data with which you'd be interacting while using AJAX. We've taken data from our server
and put it into a page without refreshing, we've queried a web service to get RSS
headlines, and we've checked usernames to make sure they're available. At this point,
you've got the basic tools that you need in order to write applications that leverage the
power of AJAX. There are a few things one should consider when building out from here,
which we'll highlight next.
 
Search WWH ::




Custom Search