Java Reference
In-Depth Information
This is a simple function; it lets the user know an error occurred.
Turn your attention to the checkUsername() function, as many changes were made in it.
function checkUsername()
{
var userValue = $F(“username”);
The fi rst line of code inside the function changed. It now uses Prototype's $F() function. This function
is used for form elements; you pass the element's id to the function, and it retrieves the element's value.
So $F(“username”) is the equivalent of document.getElementById(“username”).value. The for-
mer is defi nitely easier to type.
Now you determine if the user entered information into the Username fi eld. Simply compare the
userValue variable to an empty string.
if (userValue == “”)
{
alert(“Please enter a user name to check!”);
return;
}
If the user didn't enter data into the text box, ask them to in an alert box, and return from the function.
Now gather the information you need in preparation for making the request to the server. Create a
parms object, make a username property, and assign it the value contained in userValue .
var parms = new Object();
parms.username = userValue;
Now create your options object to pass to the Ajax.Request() constructor.
var options = getBasicOptions();
options.onSuccess = checkUsername_callBack;
options.parameters = parms;
You fi rst call getBasicOptions() to create the basic options. Then you create the onSuccess and
parameters properties; you set the former's value to a pointer to checkUsername_callback() and the
latter to the parms object you previously created.
new Ajax.Request(“formvalidator.php”, options);
}
As the last step in this function, you call the Ajax.Request() constructor, prepended by the new key-
word, and pass the URL to formvalidator.php and the options object.
If this request fails, then request_onfailure() executes. However, checkUsername_callback()
executes on a successful request, and this function saw a few changes.
The fi rst change is the parameter name; instead of data, it is now request to better refl ect that it now
contains the XMLHttpRequest-like Ajax.Response object.
function checkUsername_callBack(request)
{
Search WWH ::




Custom Search