Form Object

By using the form you have at your disposal information about the elements in a form and their values. You can alter many of these of these values as needed.

A separate instance of the form object is created for each form in a document. Objects within a FORM can be referred to by a numeric index or be referred to by name.

<FORM
     [NAME="formName"]
     [TARGET="frameName or windowName"]
     [ACTION="CGI path"]
     [METHOD="GET | POST"]
     [ENCTYPE="MIMEType"]
     [onSubmit="Event_Handler_Code"]
     [onReset="Event_Handler_Code"]>
</FORM>

Event Handlers

  • onReset - Invoked just before the elements of the FORM are Reset. Initiated by the Reset Button.

  • onSubmit - Invoked just before the FORM is submitted. This Event Handler allows form entries to be validated before being submitted. Initiated by the Submit Button.

Methods

There are two Methods available with the FORM Object:

  • reset() - reset each of the FORM 's elements to their default values

  • submit() - submit the FORM

Properties

Properties of the Form Object

PROPERTY

DESCRIPTION

ACTION

String containing the value of the ACTION attribute of the <FORM> tag

elements[ ]

Array containing an entry for each element in the form (such as checkboxes, text field, and selection lists)

elements.length

The number of items in the elements[ ] Array.

ENCODING

String containing the MIME type used for encoding the form contents sent to the server. Reflects the ENCTYPE attribute of the <FORM> tag

length

The number of elements in the form. Equivalent to elements.length.

METHOD

Specifies the technique for submitting the FORM. Values: GET or POST

NAME

String containing the value of the NAME attribute of the <FORM> tag

TARGET

String containing the name of the window targeted by a form submission

ACTION

With this Property, you can ascertain the ACTION specified in the FORM definition. For instance, in a form defined with the following:

<FORM METHOD="POST" ACTION="/cgi-bin/test.pl">

the ACTION property has a value of "/cgi-bin/test.pl".

ENCODING

The encoding property reflects the MIME type, which is used to encode the data submitted from a FORM to the server. This means that the property reflects the ENCTYPE attribute of the <FORM> tag, and you can set the encoding of a FORM by changing the value of this property.

The default value is "application/x-www-form-urlencoded" which is sufficient for almost all purposes. For example, a value "text/plain" is convenient when the FORM is being submitted by email to mailto:URL

This is useful when you want to upload a file to be processed by a CGI script on the server. For information go to http://www.ics.uci.edu/pub/ietf/html/rfc1867.txt.

METHOD

A read/write string that specifies the technique for submitting the form. It should have the value "GET" or "POST". Initially specified by the METHOD attribute.

<FORM METHOD="POST" ACTION="/cgi-bin/test.pl">

NAME

This property provides the programmer with the name specified in the form definition. In the form defined with the

<FORM NAME="myForm" METHOD="POST" ACTION="/cgi-bin/test.pl">

the NAME Property has a value of "myForm".

TARGET

The target property is similar to the ACTION and NAME Properties and makes the content of the TARGET attribute available to the programmer.  A read/write string that specifies the name of the frame or window in which the results of submitting a form should be displayed. Initially specified by the TARGET attribute. The special name "_top", "_parent", "_self", and "_blank" are also supported for the TARGET Property and the TARGET attribute (more of this will be covered in Week 8). In the FORM definition

<FORM NAME="myForm" TARGET="thatFrame" METHOD="POST" ACTION="/cgi-bin/test.pl">

the TARGET Property has a value of "thatFrame".

It is possible to dynamically change the target of a form by assigning a new value to the TARGET Property. In the preceding example, the target could be changed from thatFrame to anotherFrame by using document.myForm.target="anotherFrame".

NOTE: TARGET can only be assigned a string literal. It cannot be assigned an expression or a variable.