Interactive forms let web page authors give their pages interactive elements, such as for receiving messages from their readers, much like the reply cards found in some magazines.
The reader enters information by filling in fields or clicking on buttons, then pressing a submit button to send it to a URL, normally one pointing to an e-mail address or to a dynamic web script like PHP, ASP, or a CGI script.
Forms are delimited with the <FORM> ... </FORM> tag, which contains several form elements, such as buttons and text boxes, and which must possess the following attributes:
This is the syntax for the FORM tag:
<FORM METHOD="POST" or "GET" ACTION="url" ENCTYPE="x-www-form-urlencoded"> ... </FORM>
Here are a few examples of FORM tags:
<FORM METHOD=POST ACTION="mailto:webmaster@kioskea.net">
<FORM METHOD=GET ACTION="http://en.kioskea.net/cgi-bin/script.cgi">
The FORM tag acts as a sort of container for storing elements which let the user select or enter data. All data will be sent to the URL indicated in the ACTION attribute of the FORM tag by the method indicated by the METHOD attribute.
Any basic HTML element may be inserted into a FORM tag (such as text, buttons, tables, and links), but the interactive elements are the most important. These interactive elements are:
When a form is submitted (by clicking on the submit button), the data in the form is sent to a CGI script in the form of name/value pairs, which are sets of data represented as the name of the form element, an equal sign ("="), and then the associated value. These name/value pairs are set off from one another by ampersands ("&"). So the data sent to the script will look like this:
field1=value1&field2;=value2&field3;=value3With the GET method (sending data using a URL), the URL will be a string that looks like this:
http://en.kioskea.net/cgi-bin/script.cgi?field1=value1&field2;=value2
The INPUT tag is the essential form tag, since it is used to create many interactive "elements." This tag's syntax is as follows:
<INPUT type="Field Name" value="Default Value" name="Element Name">The name attribute is essential, as it lets the CGI script know which field is associated with which name/value pair, meaning that the name of the field will be followed by an equal sign ("=") followed by the value entered by the user, or if the user didn't input a value, by the default value for the value attribute.
The TEXTAREA tag is for defining a textbox bigger than just the single line allowed by the INPUT tag. This tag has the following attributes:
The SELECT tag is for creating a dropdown list of elements (specified by the OPTION tags within it). This tag's attributes are:
Forms can be placed in a page using tables (recommended for a professional-looking layout). Here is an example summing up the above points, showing you how to place a form in a web page using a table:
<FORM method=post action="cgi-bin/script.pl">
User Registration
<TABLE BORDER=0>
<TR>
<TD>Last Name</TD>
<TD>
<INPUT type=text name="lastname">
</TD>
</TR>
<TR>
<TD>First Name</TD>
<TD>
<INPUT type=text name="firstname">
</TD>
</TR>
<TR>
<TD>Gender</TD>
<TD>
Man: <INPUT type=radio name="gender" value="M">
<br>Woman: <INPUT type=radio name="gender" value="F">
</TD>
</TR>
<TR>
<TD>Occupation</TD>
<TD>
<SELECT name="occupation">
<OPTION VALUE="teacher">Teacher</OPTION>
<OPTION VALUE="student">Student</OPTION>
<OPTION VALUE="engineer">Engineer</OPTION>
<OPTION VALUE="retired">Retired</OPTION>
<OPTION VALUE="other">Other</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD>Comments</TD>
<TD>
<TEXTAREA rows="3" name="comments">
Type your comments here</TEXTAREA>
</TD>
</TR>
<TR>
<TD COLSPAN=2>
<INPUT type="submit" value="Send">
</TD>
</TR>
</TABLE>
</FORM>
Here's what shows up on screen:
| Tag | Attribute | Value | Result | Visual Effect |
|---|---|---|---|---|
| <FORM> ... </FORM> | METHOD | POST GET |
||
| ACTION | Sends to the address shown | |||
| ENCTYPE | Specifies encoding type | |||
| <INPUT> | TYPE | submit | performs the ACTION in the <FORM> tag | |
| text | simple line of text whose length is given by the SIZE attribute |
|||
| reset | Erases the form's contents | |||
| radio | radio button | |||
| checkbox | checkbox | |||
| NAME | Name | |||
| SIZE | Text size | |||
| <TEXTAREA> ... </TEXTAREA> | NAME | Text box | ||
| ROWS | ||||
| COLS | ||||
<SELECT>
</SELECT> |
NAME | |||
| MULTIPLE | Multiple selections possible | |||
| <OPTION> ... </OPTION> | SELECTED | Default choice | ||
| VALUE | Forced value |