SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 226 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

jquery.com/jquery-latest.pack.js">
If you??™re building an internal application where your users won??™t have access to the Internet,
you can download jQuery from http://jquery.com.
JSON is a convenient way to send small amounts of data back and forth to the server from
JavaScript. You??™ll also use a library called Services_JSON (http://mike.teczno.com/json.html)
that enables you to return JSON from the server in a convenient way so that you don??™t have to
worry about the detail. Since you??™re sending only a field name and a value to the server, passing
this information in the query string is the most convenient way. The server URL needs to
extract this information from the query string, call the validate() function to validate it, and
then return the results in the JSON format. In PHP, it would look something like Listing 7-7.
Listing 7-7. ajax-validate.php calls the validate() function and returns the result as JSON
require_once "validation.php";
require_once "JSON.php";
$field = $_GET["field"];
$value = $_GET["value"];
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 163
if (!isset($field) || !isset($value)) {
return_json("");
die();
}
$check = validate($field, $value);
return_json($check);
die();
function return_json($data) {
$json = new SERVICES_JSON();
echo $json->encode($data);
}
?>
To test the Services_JSON PHP library visit the validate URL directly in your browser.


Pages:
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238