Java Reference
In-Depth Information
</tr>
<tr>
<td colspan=”2” class=”submit”>
<input type=”submit” value=”Submit” />
</td>
<td />
</tr>
</table>
</form>
</body>
</html>
Save this fi le as
ch15_examp8.htm
, and save it in your web server's root directory. Open and point your
browser to
http://yourserver/ch15_examp8.htm
and test it. You'll fi nd that it behaves just as all the
previous versions did.
The usual suspects, checkUsername(), checkUsername_callback(), checkEmail(), and
checkEmail_callback(), all consist of changes. You also added two new functions:
request_onfailure() and getBasicOptions().
Let's examine the new functions fi rst, starting with
getBasicOptions()
. Like the Prototype version
of this script, you created this function to generate an
options
object with the settings all requests
will use. This saves you time and keystrokes, as you only have to write them once and call the function
whenever you need it. The function defi nition follows:
function getBasicOptions()
{
var options = new Object();
options.method = “get”;
options.onFailure = request_onfailure;
options.url = “formvalidator.php”;
return options;
}
In this function, you create the options object and create the method, onFailure, and url proper-
ties. You assign them the following values: get, request_onfailure, and formvalidator.php. The
XMLHttpRequest object will make a GET request to formvalidator.php, and the request_onfailure()
function will execute only when the request object encounters an error.
The
request_onfailure()
function simply tells the user an error occurred, as the following code
shows:
function request_onfailure(request)
{
alert(“An error occurred. HTTP Status Code: “ + request.status);
}
You display the status code, as it could be helpful debugging the script if an error does indeed occur
with the request.
The
checkUsername()
and
checkUsername_callback()
functions underwent the same changes as in
the previous versions. So you'll only look at the lines that changed. The fi rst change in
checkUsername()
is how the information entered into the
Username
fi eld is retrieved.
var userValue = $(“username”).value;