Java Reference
In-Depth Information
alert("Please enter a user name to check!");
return;
}
You use
$()
to select the appropriate
<input/>
element and call the
val()
method. This retrieves the
value of the form control and assigns it to the
userValue
variable.
After you validate the user's input, you're ready to start issuing a GET request to the server. First, you
create an object to contain the information you want to send to the server:
var parms = {
username: userValue
};
You call this object
parms
and populate it with the username property. As you learned earlier in this
chapter, jQuery will add this property and its value to the query string.
Now, you can send the request using jQuery's
getJSON()
method:
$.getJSON("ch14_formvalidator.php", parms).done(handleResponse);
}
You add the
handleResponse()
function to the “done” queue, so that when the request successfully
completes, an
alert
box will display the search results.
The new
checkEmail()
function is very similar to
checkUsername()
. The two main differences, of
course, are the data you retrieve from the form and the data you send to the server:
function checkEmail(e) {
e.preventDefault();
var emailValue = $("#email").val();
if (!emailValue) {
alert("Please enter an email address to check!");
return;
}
var parms = {
email: emailValue
};
$.getJSON("ch14_formvalidator.php", parms).done(handleResponse);
}
The final function,
handleResponse()
, is mostly unchanged from the original version. Because
jQuery's
getJSON()
method automatically parses the response into a JavaScript object, the new
handleResponse()
function simply uses the passed data as‐is:
function handleResponse(response) {
if (response.available) {
alert(response.searchTerm + " is available!");
} else {