|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Javascript wears many hats. You can use JavaScript to create special effects. You can use JavaScript to make your HTML pages "smarter" by exploiting its decision-making capabilities. And you can use JavaScript to enhance HTML forms. This last application is of particular importance. Of all the hats JavaScript can wear, its form processing features are among the most sought and used.
Nothing strikes more fear in the heart of a Web publisher than these three letters: C-G-I. CGI (which stands for common gateway interface), is a mechanism for safely transporting data from a client (a browser) to a server. It is typically used to transfer data from an HTML form to the server.
With JavaScript at your side, you can process simple forms without invoking the server. And when submitting the form to a CGI program is necessary, you can have JavaScript take care of all the preliminary requirements, such as validating input to ensure that the user has dotted every i. In this column we'll look closely at the JavaScript-form connection, including how to use JavaScript's form object, how to read and set form content, and how to trigger JavaScript events by manipulating form controls. We'll also cover how to use JavaScript to verify form input and sumbit the form to a CGI program.
This article is part of the JavaWorld technical content archive. You can learn a lot about JavaScript programming by reading articles in the JavaScript series, just keep in mind that some of the information is likely to be outdated. See "Using JavaScript's built-in objects" and "Debugging JavaScript programs" for more about programming with JavaScript.
There are few differences between a straight HTML form and a JavaScript-enhanced form. The main one being that a JavaScript form relies on one or more event handlers, such as onClick or onSubmit. These invoke a JavaScript action when the user does something in the form, like clicking a button. The event handlers, which are placed with the rest of the attributes in the HTML form tags, are invisible to a browser that don't support JavaScript. Because of this trait, you can often use one form for both JavaScript and non-JavaScript browsers.
Typical form control objects -- also called "widgets" -- include the following:
I won't bother enumerating all the attributes of these controls (widgets), and how to use them in HTML. Most any reference on HTML will provide you with the details. For use with JavaScript, you should always remember to provide a name for the form itself, and each control you use. The names allow you to reference the object in your JavaScript-enhanced page.
The typical form looks like this. Notice I've provided NAME= attributes for all form controls, including the form itself:
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
</FORM>
Let's experiment with obtaining values from form objects. Load the page, then type something into the text box. Click the button, and what you typed is shown in the alert box.