Java Reference
In-Depth Information
}
function handleEmailResponse(transport) {
var response = transport.responseJSON;
if (!response.available) {
alert("The email address " + response.searchTerm +
" is unavailable. Try another.");
return;
}
document.theForm.submit();
}
function handleError() {
alert("A network error occurred. Please try again " +
"in a few moments.");
}
$("btnSubmit").observe("click", btnSubmitClick);
</script>
</body>
</html>
Save this as
ch17 _ question1.html
.
This solution is based on Chapter 14's Solution 2, but the code has changed to use Prototype's API.
There's also a new function called
handleError()
for handling errors:
function handleError() {
alert("A network error occurred. Please try " +
"again in a few moments.");
}
This function is called
handleError()
, and it simply displays a message to the user. You'll assign
this function to the
onFailure
option when you make your Ajax requests.
Making a request is many more lines of code due to Prototype's Ajax API:
var options = {
method: "get",
onSuccess: handleEmailResponse,
onFailure: handleError,
parameters: {
email: emailValue
}
};
new Ajax.Request("ch14_formvalidator.php", options);
This excerpt is taken from
checkUsername()
. You create your
options
object containing the
method
,
onSuccess
,
onFailure
, and
parameters
properties, and then you issue the request.