Form
Form
Introduction
In computer science, a form is a screen display in which user is expected to fill in information and then
submit to a program after completion. A form comprises a complex yet flexible framework to allow
users basic controls. It provides an interactive and user-friendly interface to user by placing a handful of
GUI (Graphical User Interface) controls on the user agent to allow the user to enter data.
In web authoring, a form is an element in a web page which is used to provide input from user back to
scripts or to submit data to the server for further process.
<FORM> Tag
A form is inserted unto the web document by placing form fields within <form> tags. The entire form
or any of the tags can be formatted like any other element in the document, and can be placed within any
element capable of holding other elements.
Usage:
<form action=”url_of_handler” method=”get | post”>
Attribute:
action: The URL of the handler which the destination of the data and where the data is processed in the next step.
method: It controls how the data is sent to the handler.
Possible values: get | post
Example:
<html>
<head></head>
<body>
<form action=”forum.php” method=”get”>
<label for=”Name”>Name</label>
<input type=”text” name=”Name” size=”50”><br>
<label for=”Name”>Message</label>
<textarea name=”Msg” cols=”50” rows=”10”>Your message
here.</textarea><br>
<input type=”submit”>
</form>
</body>
</html>
OptionButton1
1
OptionButton2
OptionButton3
✘ CheckBox1
✘ CheckBox2
CheckBox3
Attirbute:
name: The name of the check boxes.
value: It is the value of the check boxes group which is sent to the handler. If this attribute is not set, the
selected check boxes are given the value “on”.
F.4 CIT – HTML – Form page 4
checked: The radio button which has this attribute will be checked. In XHTML, this attribute is written as
checked=”checked”
Example:
<p>Interest:
<input type=”checkbox” name=”sports”>Sports
<input type=”checkbox” name=”reading”>Reading
<input type=” checkbox” name=”others”>Others
</p>
5. List Boxes
List boxes are used to allow a user to pick one or more textual items from a list. The list can
be presented in it entirely, with each element visible or as a pull-down list where the user can scroll
to their choices.
<option>Wednesday
<option>Thursday
<option>Friday
<option>Saturday
<option>Sunday
</selected>
Variation:
Occasionally, one might want to group options of a list together for clarity. <optgroup> tag can serve
such purpose. The <optgroup> tag encapsulates items that should be in that group. The group is visibly
divided by the name of the option group which is set in the label attribute of <optgroup> tag. Although the
group name is shown in the list box, it cannot be selected.
Example:
<select name=”weekdays” size=”4” multiple>
<optgroup label=”Weekday”>
<option selected>Monday
<option>Tuesday
<option>Wednesday
<option>Thursday
<option>Friday
</optgroup>
<optgroup label=”Weekend”>
<option>Saturday
<option>Sunday
</optgroup>
</selected>
6. Text Areas
For large pieces of text, one can use <textarea> tag. This tag defines a text area which can
accept textual input of up to 1024 characters and uses a multiline text box for input.
Text, Text, Text, Text, Text,
Text, Text, Text, Text, Text,
Text, Text, Text, Text, Text,
Text, Text, Text, Text, Text,
Text, Text, Text, Text, Text,
Text, Text, Text, Text, Text,
Figure 5. A text area
Usage:
<textarea name=”field_name” cols=”cols_no” rows=”rows_no”>
[Initial content]</textarea>
Attribute:
name: The name of the text area.
cols: The number of visible columns.
F.4 CIT – HTML – Form page 6
Note that the format of the text which is placed between the <textarea> tags is preserved.
Therefore, for example, the following code will produce a newline character, rather than a blank
text area.
<textarea>
</textarea>
In addition, the text entered into the <textarea> field wraps within the width of the box, but
these automatically created newline characters will not be sent to the handler.
7. Buttons
A custom button can be added to the form by using <input type=”button”>.
CommandButton1
Figure 6. A button
Usage:
<input type=”button” name=”button_name” value=”Caption”>
Attribute:
name: The name of the button.
value: The caption of the button.
Example:
<input type=”button” name=”helloBtn” value=”Press Me!”>
However, buttons by themselves are useless on a form. To have the button actually do something,
the button should be linked to a script via the event handler. For example:
<input type=”button” name=”helloBtn” value=”Press Me!”
onclick=”JavaScript:alert(‘Hello!’)”>
Variations
Submit Button:
A submit button allows users to submit the data entered to the handler.
The format of a submit button:
<input type=”submit” [value=”caption”]>
Reset Button”
A reset button resets the form to its default state. That is, it will reset all controls to their initial states.
The format of a reset button:
F.4 CIT – HTML – Form page 7
Common Attributes
Several attributes are shared among controls.
1. name / id: These two attributes are the identity of the controls. However, they have
difference which is significant when writing scripts and CSS.
In fact, name and id attributes are applicable to almost all HTML tags.
2. tabindex: It defines what order the fields are selected in when the user presses the Tab key.
This attribute takes a numeric argument tags specifies field’s order in the form.
3. accesskey: It defines a key that user can press to directly access the field. This attribute takes a
single letter, which becomes the shortcut to the field as an argument.
However, accesskey attribute usually require an additional key to be pressed
with the access key. For example, user agents running on Windows require Alt key
to be pressed along with the letter specified.
Example:
<input type=”text” id=”fn” name=”fn” tabindex=”3” accesskey=”F” size=”30”>
4. readonly: This attribute keeps the users from being able to edit the data contained in the
controls. This attribute does not take any argument.
5. disabled: This attribute effectively disables a control (usually greying out the control) so
the user cannot use the control.
F.4 CIT – HTML – Form page 8
F.4 CIT – HTML – Form page 9
Example:
<input type=”text” size=”20” value=”Read only field” readonly>
OptionButton2
OptionButton3
Figure 7. Fieldset and Legent