Forms
Forms
The method
Attribute
There are two possible values for the method
attribute: get or post.
the get method (the default) packages the form data
by appending it to the end of the URL specified in the
action attribute
the post method sends form data in a separate data
stream, allowing the Web server to receive the data
through what is called standard input
the post method is considered the preferred way of
sending data to a Web server
The post method is also safer, because some Web
servers limit the amount of data sent via the get
method and will truncate the URL, cutting off valuable
information
This figure shows the CGI script that processes the form is located at the URL
http://www.langear.com/cgi/mailer (a fictional address) and uses the post method.
Form
Indicates the beginning and end of a form
There can be more than one form in an HTML
document
However, forms cannot be nested and it is
important that they should not overlap.
Attributes
action=url
Specifies the URL of the application that will process
the form (required). The default is the current URL.
enctype=encoding
method=get|post
Specifies which HTTP method will be used to submit
the form data (required).
With get (the default), the information is appended
to and sent along with the URL itself.
The post method puts the form information in a
separate part of the body of the HTTP request.post is
the preferred method according to the W3C
specification.
target=name
<input type=button>
Attributes
name=string
Assigns a name to the form element to be passed to the
forms processing application.
value=string
Specifies the initial value for the parameter.
10
11
12
<input type=checkbox>
Attributes
checked
When this attribute is added, the checkbox will be
checked by default.
name=text
Assigns a name to the checkbox to be passed to the
form-processing application if selected.
Giving the same name to several checkboxes creates
a group of checkbox elements, allowing users to select
several options with the same property.
value=text
Specifies the initial value of the control that is passed
on to the server. If not defined, a value of "on" is sent.
Creating File
Buttons
A file button is used to select files so
that their contents can be submitted for
processing to a CGI script.
The contents of the file are not
displayed--only the files location.
A programmer can then use that
information to retrieve a file and use it
for whatever purpose is required by the
script.
14
Using a File
Button
The figure
shows an
example of
using the file
button to
return the
location of a
file named
report.doc.
15
<input type=file>
Allows users to submit external files with their form
submission. It is accompanied by a "browse button
when displayed in the browser.
Attributes
accept=MIME type
Specifies a comma-separated list of content types
that a server processing the form will handle
correctly. It can be used to filter out nonconforming
files when prompting a user to select files to send to
the server.
name=text
Assigns a name to the control.
value=text
Specifies the initial filename to be submitted.
17
18
<input type=hidden>
Creates an element within a <form> that does not
display in the browser. Hidden controls can be
used to pass special form-processing information
along to the server that the user cannot see nor
alter.
Attributes
name=text
Specifies the name of the parameter that is
passed to the form-processing application for this
input element (required).
value=text
Specifies the value of the element that is passed
to the form-processing application.
Creating a Password
Field field is a text box in which
A password
20
<input type=password>
Creates a text-input element (like text) but the input
text is rendered in a way that hides the characters,
such as displaying a string of asterisks (*) or bullets
(o).
NOTE: this does not encrypt the information and
should not be considered as a real security measure.
Attributes
maxlength=number
Specifies the maximum number of characters the
user can input for this element.
name=text
Specifies the name of this parameter to be passed to
the form-processing application for this element
(required).
size=number
Specifies the size of the text-entry box (measured
in number of characters) to be displayed for this
element. Users can type entries that are longer
than the space provided, causing the field to
scroll to the right.
value=text
Specifies the value that will initially be displayed
in the text box.
23
Creating Radio
Buttons
This figure shows an example of HTML code that creates radio buttons for party affiliations.
In this sample code, the value sent to the CGI script does not match the field label.
If the user selects the Republican radio button, the value gop
is sent to the CGI script paired with the field name party.
24
<input type=radio>
Creates a radio button that can be turned on and off. When a
group of radio buttons share the same control name, only one
button within the group can be "on" at one time and all the
others will be turned "off." This makes them different from
checkboxes, which allow multiple choices to be selected within
a group.
Attributes
checked
Causes the radio button to be in the "on" state when the form is
initially displayed.
name=text
Specifies the name of the parameter to be passed on to the
forms-processing application if this element is selected
(required).
value=text
Specifies the value of the parameter to be passed on to the
forms-processing application.
Creating a Group
Box
start of
group box
group box
legend
28
Creating Form
Buttons
Buttons
can be clicked to:
run programs
submit forms
reset the form to its original state
30
31
32
33
34
Creating Buttons
with the <button> Tag
Buttons created with the <input> tag do not allow the
Web page designer to control the appearance of the
button.
For greater artistic control over the appearance of the
button, use the <button> tag.
The syntax of the <button> tag is:
<button name=name value=value type=option>
button text and HTML tags
</button>
name attribute specifies the name of the button
value attribute sends to a CGI script
type attribute specifies the button type (submit, reset,
or button)
35
contents
of the
button
button image
36
<input type=reset>
Creates a reset button that clears the contents (or
sets them to their default values) of the elements
in a form.
Attributes
value=text
Specifies alternate text to appear in the button.
<input type=submit>
Creates a submit button that sends the
information in the form to the server for
processing.
Attributes
value=text
Specifies alternate text to appear in the button.
text box
39
40
This figure
shows an
example of code
for setting the
width of text
boxes.
41
42
43
Setting a Default
Value for a Field
When the same value is entered into a
field, it may make sense to define a
default value for a field.
Default values can save time and
increase accuracy for users of a Web
site.
To define a default value, use the
following syntax:
<input value=value>
value is the default text or number that is
displayed in the field
44
default value
45
<input type=text>
Creates a text input element. This is the default input
type and certainly the most useful and common.
Attributes
maxlength=number
Specifies the maximum number of characters the user
can input for this element.
name=text
Specifies the name of this parameter to be passed to the
form-processing application for this element (required).
size=number
Specifies the size of the text-entry box (measured in
number of characters) to be displayed for this element.
Users can type entries that are longer than the space
provided, causing the field to scroll to the right.
value=text
Specifies the value that will initially be displayed in the
text box.
47
value of the id
attribute for the first
name field
48
Labels
Used to attach information to controls. Each label
element is associated with exactly one form
control.
Attributes
for=text
Explicitly associates the label with the control by
matching the value of the for attribute with the
value of the id attribute within the control
element.
Example:
<LABEL for="lastname">Last Name: </LABEL>
<INPUT type="text" id="lastname" size="32">
Creating a Text
Area
50
Creating a Text
Area
This figure shows
an example of a
text area with
default text.
The
<textarea>
tag is a two-sided
tag, which means
that it has an
opening tag
<textarea>,
and a closing tag,
</textarea>.
default text
area text
dimensions
of text area
51
Wrap Attribute
Values
The text entered in a text area wraps to the next line when it exceeds the width of the
box. To control how a browser wraps text to a new line use the wrap attribute.
This figure shows the three possible wrap options.
52
53
Comment Text
Area
54
Textarea
Defines a multiline text-entry control.
The text that is enclosed within the <textarea> tags will be
displayed in the text-entry field when the form initially
displays.
Attributes
cols=number
Specifies the visible width of the text-entry field, measured in
number of characters (required).
Users may enter text lines that are longer than the provided
width, in which case the entry will scroll to the right (or wrap
if the browser provides some mechanism for doing so).
name=text
Specifies a name for the parameter to be passed to the formprocessing application (required).
rows=number
Specifies the height of the text-entry field in numbers of lines of
text (required). If the user enters more lines than are visible, the
text field scrolls down to accommodate the extra lines.
wrap=off|virtual|physical
Sets word wrapping within the text area.
Off: turns word wrapping off; users must enter their own line
returns.
Virtual: displays the wrap, but the line endings are not
transmitted to the server.
Physical: displays and transmits line endings to the server.
Soft: same as virtual.
Hard: same as physical.
Creating a Selection
List
57
58
Creating a
Selection List
59
Using a Selection
List
60
Modifying the
Appearance
HTML provides several attributes to modify
of
Selection
List
the a
appearance
and behavior
of selection lists
and options.
By default, the <select> tag displays one
option from the selection list, along with a list
arrow to view additional selection options.
The number of options displayed can be
modified with the size attribute.
The syntax of the size attribute is:
<select size=value>
61
Modifying the
Appearance
value is the number of items that the selection list
in the form
of displays
a
Selection
List
by specifying a value greater than 1, the selection
list changes from a drop-down list box to a list box
Continued
if the size attribute is equal to the number of
options in the selection list, the scroll bar is either
not displayed or is dimmed
62
size = "1"
size = "7"
size = "4"
size = "9"
63
Making Multiple
Selections
When using multiple selections, users are not
limited to a single selection from a selection
list.
Adding the multiple attribute to the
<select> tag allows multiple selections from
a list.
The syntax for this attribute is:
<select multiple>
A common method to make multiple
selections from a selection list is to hold down
a specific key while making selections.
64
Making Multiple
Selections Continued
65
66
Option Groups
option group
label
a single
option
group
option
group label
a single option
group
68
Optgroup
Defines a logical group of <options>.
This could be used by browsers to display
hierarchical cascading menus.
<optgroups> cannot be nested.
Attributes
label=text
Specifies the label for the option group (required).
Option
Defines an option within a select element (a
multiple-choice menu or scrolling list).
Attributes
selected
Makes this item selected when the form is initially
displayed.
value=text
Returns a specified value to the forms-processing
application instead of the <option> contents.
Select
Defines a multiple-choice menu or a scrolling list. It is a
container for one or more <option> tags.
Attributes
multiple
This allows the user to select more than one <option> from
the list.
name=text
Defines the name for selected <option> values that, if
selected, are passed on to the forms processing application
(required).
size=number
Controls the display of the list of options. When size=1 (and
multiple is not specified), the list is displayed as a pull-down
menu. For values higher than 1, the options are displayed as
a scrolling list with the specified number of options visible.
Pull-down menus
The <Select> element displays as a pull-down
menu of options when no size specification is
listed(the default) or when size=1.
In Pull-down menu ,only one item may be
selected at a time .
(note: adding multiple attribute )