Building Forms with JavaScript

From Techotopia
Revision as of 17:19, 15 May 2007 by Neil (Talk | contribs) (The JavaScript Form Object)

Jump to: navigation, search

The ability to interact with, and obtain information from web site visitors, is a central theme behind the JavaScript language. No JavaScript book would be complete, therefore, without exploring how to handle Graphical User Interface Componments (GUI) objects such as buttons, text input fields, checkboxes, radio boxes and selections, together with the Form object which brings them all together.

The JavaScript Form Object

The JavaScript Form object serves a number purposes. It acts a container for all of the GUI objects that make up a form. It also acts as the mechansism for grouping together the information provided by the user and sending it back to the server for processing.

The <form> takes a number of attributes of which the following are the most commonly used:

  • name - the name of the form, which will be used when referencing objects within the the form four JavaScript code.
  • method - the method by which data is sent to the server. This will either be "GET" ot "POST" depdniong on the server-side configuration.
  • action - the URL of the CGI script to which the data entered by the user will be sent. It is also possible to use mailto: to have the data sent to an email address.

The following is an example of a Form object named registerForm which submits the user data to a cgi script called register.cgi located in the /cgi-bin directory on the server which hosts the web site. The transfer is configured to use the GET method:


<form name="registerForm" method="GET" action="/cgi-bin/register.cgi">

<-- Form Objects go here -->

</form>

The JavaScript Text Object

Now that we have covered the basics of the JavaScript Form Object it is time to look at the objects we can place into the Form container to interact with the user. The most common of these objects is the text object. The text