Java Reference
In-Depth Information
alert(data.searchTerm + " is available!");
} else {
alert("We're sorry, but " + data.searchTerm + " is not available.");
}
}
$("usernameAvailability").addEvent("click", checkUsername);
$("emailAvailability").addEvent("click", checkEmail);
</script>
</body>
</html>
Save this file as
ch17 _ example7.html
, and save it in your web server's root directory. Open and point
as all the previous versions did.
This version is very similar to Example 4—the Prototype version. In fact,
checkUsername()
and
checkEmail()
are identical to Example 4 except for the request code. So let's just look at that, starting
with
checkUsername()
.
After you get and validate the user input for the username, you build your
options
object:
var options = {
url: "ch14_formvalidator.php",
data: {
username: userValue
},
onSuccess: handleResponse
};
You set the
url
,
data
, and
onSuccess
properties and pass the object to the
Request.JSON()
constructor:
new Request.JSON(options).get();
And to save some typing, you chain the
get()
call to the
Request.JSON
constructor.
The code inside
checkEmail()
is unsurprisingly similar (at this point, what is about this example?).
First, you build your
options
object:
var options = {
url: "ch14_formvalidator.php",
data: {
email: emailValue
},
onSuccess: handleResponse
};
Then you send the request:
new Request.JSON(options).get();