11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 22 • CREATING AJAX-ENHANCED FEATURES WITH JQUERY AND <strong>PHP</strong>www.it-ebooks.info$("#author_name").click(function(){$("#author_bio").toggle();});So far you’ve learned how jQuery can conveniently associate events with elements, as well as parseand manipulate the DOM in a variety of ways. In the two examples which follow, you’ll use theseconcepts as well as a few other features to create two Ajax-driven features, beginning with the usernameexistence validation feature that earlier examples alluded to.Creating a Username Existence ValidatorThere are few tasks more frustrating than repeatedly being told a particular username exists whencreating a new e-mail address or account, particularly on a popular website such as Yahoo! where itseems as if every possible combination has already been taken. To reduce the frustration, websites havestarted taking advantage of Ajax-enhanced registration forms which will automatically check for ausername’s existence before the form is even submitted (see Figure 22-3), notifying you of the result. Insome instances, if a username is taken, the website will suggest some variations which the registrantmight find appealing.Figure 22-3. Yahoo’s Username ValidatorLet’s create a username validator that closely resembles the version implemented by Yahoo! inFigure 22-3. In order to determine whether a username already exists, you will need a central accountrepository from which to base the comparisons. In a real-world situation, this account repository willalmost certainly be a database; however because you haven’t yet delved into that topic yet, an array willbe used instead for illustrative purposes.Begin by creating the registration form (register.php), presented in Listing 22-1.Listing 22-1. The Registration Form

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!