AJAX - The Complete Reference

(avery) #1

388 Part II: Developing an Ajax Library^


In PHP, it is possible to write a simple script to check format validity of a passed ZIP value:

<?php
header("Cache-Control: no-cache");
header("Pragma: no-cache");
$zip = htmlentities(substr(urldecode($_GET['zip']),0,16));
if (preg_match("/(^\d{5}$)|(^\d{5}-\d{4}$)/", $zip))
print "valid";
else
print "notvalid";
?>

Then Ajax can be used to make a call to this PHP script and provide error messages. Of
course, there really is no reason to go to the network all the time for such a simple check.
This makes Ajax nearly as inefficient as its form-posting predecessor, as shown in Figure 8-13,
which shows numerous round trips during a user session.
Now, it would be easy enough to perform the kind of format validation that was
performed server side in client-side JavaScript. We hope readers already use this kind of

FIGURE 8-13 Ajax isn’t appropriate for every task
Free download pdf