Forms in HTML documents
previous
next
contents
elements
attributes
index
17
Forms
Contents
Introduction to forms
Controls
Control types
The
FORM
element
The
INPUT
element
Control types created with
INPUT
Examples of forms containing INPUT
controls
The
BUTTON
element
The
SELECT
OPTGROUP
, and
OPTION
elements
Pre-selected options
The
TEXTAREA
element
The
ISINDEX
element
Labels
The
LABEL
element
Adding structure to forms: the
FIELDSET
and
LEGEND
elements
Giving focus to an element
Tabbing navigation
Access
keys
Disabled and read-only controls
Disabled controls
Read-only controls
Form submission
Form submission method
Successful controls
Processing form data
Step one: Identify the successful
controls
Step two: Build a form data
set
Step three: Encode the form data
set
Step four: Submit the encoded form
data set
Form content types
application/x-www-form-urlencoded
multipart/form-data
17.1
Introduction to forms
An HTML form is a section of a document containing normal content, markup,
special elements called
controls
(checkboxes, radio buttons, menus, etc.), and labels on those controls. Users
generally "complete" a form by modifying its controls (entering text, selecting
menu items, etc.), before submitting the form to an agent for processing (e.g.,
to a Web server, to a mail server, etc.)
Here's a simple form that includes labels, radio buttons, and push buttons
(reset the form or submit it):












Male

Female




Note.
This specification includes more detailed
information about forms in the subsections on
form display issues
17.2
Controls
Users interact with forms through named
controls
A control's
"control name"
is given by its
name
attribute. The scope of the
name
attribute for a
control within a
FORM
element is the
FORM
element.
Each control has both an initial value and a current value, both of which
are character strings. Please consult the definition of each control for
information about initial values and possible constraints on values imposed by
the control. In general, a control's
"initial
value"
may be specified with the control element's
value
attribute. However, the initial value of a
TEXTAREA
element is given by its contents, and the initial value of
an
OBJECT
element in a form is determined by the object
implementation (i.e., it lies outside the scope of this specification).
The control's
"current value"
is first set to the
initial value. Thereafter, the control's current value may be modified through
user interaction and
scripts.
A control's initial value does not change.
Thus, when
a form is reset, each control's current value is reset to its initial value. If
a control does not have an initial value, the effect of a form reset on that
control is undefined.
When a form is submitted for processing, some controls have their name
paired with their current value and these pairs are
submitted
with the form. Those controls for which name/value pairs are
submitted are called
successful
controls
17.2.1
Control
types
HTML defines the following control types:
buttons
Authors may create three types of buttons:
submit buttons
: When activated, a submit button
submits a form.
A form may contain more than one submit
button.
reset
buttons
: When activated, a reset button resets all controls to their
initial values.
push
buttons
: Push buttons have no default behavior. Each push button may
have
client-side scripts
associated with the
element's
event
attributes. When an event
occurs (e.g., the user presses the button, releases it, etc.), the associated
script is triggered.
Authors should specify the scripting language of a push button script
through a
default script declaration
(with the
META
element).
Authors create buttons with the
BUTTON
element or the
INPUT
element. Please consult the definitions of these elements for details about
specifying different button types.
Note.
Authors should note that the
BUTTON
element offers richer rendering capabilities than the
INPUT
element.
checkboxes
Checkboxes (and radio buttons) are on/off switches that may be toggled by
the user. A switch is "on" when the control element's
checked
attribute is set. When a form is submitted, only "on" checkbox controls can
become
successful
Several checkboxes in a form may share the same
control name.
Thus, for example, checkboxes allow users to select several
values for the same property. The
INPUT
element is used to create a
checkbox control.
radio
buttons
Radio buttons are like checkboxes except that when several share the same
control name
, they are mutually exclusive: when one
is switched "on", all others with the same name are switched "off". The
INPUT
element is used to create a radio button control.
If no radio button in a set sharing the same control name is initially
"on", user agent behavior for choosing which control is initially "on" is
undefined.
Note.
Since existing implementations handle this
case differently, the current specification differs from RFC 1866 (
[RFC1866]
section 8.1.2.4), which states:
At all times, exactly one of the radio buttons in a set is checked.
If none of the elements of a set of radio buttons specifies
`CHECKED', then the user agent must check the first radio button of the set
initially.
Since user agent behavior differs, authors should ensure that in each set of
radio buttons that one is initially "on".
menus
Menus offer users options from which to choose. The
SELECT
element creates a menu, in combination with the
OPTGROUP
and
OPTION
elements.
text input
Authors may create two types of controls that allow users to input text.
The
INPUT
element creates a single-line input control and the
TEXTAREA
element creates a multi-line input control. In both cases,
the input text becomes the control's
current
value
file select
This control type allows the user to select files so that their contents
may be submitted with a form. The
INPUT
element is used to create a file
select control.
hidden controls
Authors may create controls that are not rendered but whose values are
submitted with a form. Authors generally use this control type to store
information between client/server exchanges that would otherwise be lost due to
the stateless nature of HTTP (see
[RFC2616]
). The
INPUT
element is used to create a hidden control.
object controls
Authors may insert generic objects in forms such that associated values are
submitted along with other controls. Authors create object controls with the
OBJECT
element.
The elements used to create controls generally appear inside a
FORM
element, but may also appear outside of a
FORM
element declaration when they are
used to build user interfaces. This is discussed in the section on
intrinsic events.
Note that controls outside a form
cannot be
successful controls
17.3
The
FORM
element
FORM
- - (
%block;
|SCRIPT)+ -(FORM) -- interactive form -->
%attrs;
--
%coreattrs
%i18n
%events
--
action
%URI;
#REQUIRED -- server-side form handler --
method
(GET|POST) GET -- HTTP method used to submit the form--
enctype
%ContentType;
"application/x-www-form-urlencoded"
accept
%ContentTypes;
#IMPLIED -- list of MIME types for file upload --
name
CDATA
#IMPLIED -- name of form for scripting --
onsubmit
%Script;
#IMPLIED -- the form was submitted --
onreset
%Script;
#IMPLIED -- the form was reset --
accept-charset
%Charsets;
#IMPLIED -- list of supported charsets --
Start tag:
required
, End tag:
required
Attribute definitions
action
uri
[CT]
This attribute specifies a form processing agent. User agent behavior for a
value other than an HTTP URI is undefined.
method
get|post
[CI]
This attribute specifies which HTTP method will be used to submit the
form data set
. Possible (case-insensitive) values are
"get" (the default) and "post". See the section on
form submission
for usage information.
enctype
content-type
[CI]
This attribute specifies the
content type
used to submit the form to the server (when the value of
method
is "post"). The default value for this attribute is
"application/x-www-form-urlencoded"
. The value
"multipart/form-data"
should be used in combination with the
INPUT
element,
type
="file".
accept-charset
charset list
[CI]
This attribute specifies the list of
character encodings
for input
data that is accepted by the server processing this form. The value is a space-
and/or comma-delimited list of
charset
values. The client must interpret this list as an exclusive-or list, i.e., the
server is able to accept any single character encoding per entity received.
The default value for this attribute is the reserved string "UNKNOWN". User
agents may interpret this value as the character encoding that was used to
transmit the document containing this
FORM
element.
accept
content-type-list
[CI]
This attribute specifies a comma-separated list of content types that a
server processing this form will handle correctly. User agents may use this
information to filter out non-conforming files when prompting a user to select
files to be sent to the server (cf. the
INPUT
element when
type
="file").
name
cdata
[CI]
This attribute names the element so that it may be referred to from style
sheets or scripts.
Note.
This attribute has been included for
backwards compatibility. Applications should use the
id
attribute to identify elements.
Attributes defined elsewhere
id
class
document-wide identifiers
lang
language information
),
dir
text
direction
style
inline
style information
title
element
title
target
target
frame information
onsubmit
onreset
onclick
ondblclick
onmousedown
onmouseup
onmouseover
onmousemove
onmouseout
onkeypress
onkeydown
onkeyup
intrinsic events
The
FORM
element acts as a container for
controls
. It specifies:
The layout of the form (given by the contents of the element).
The program that will handle the completed and submitted form (the
action
attribute). The receiving program must be able to parse name/value pairs in
order to make use of them.
The method by which user data will be sent to the server (the
method
attribute).
A character encoding that must be accepted by the server in order to handle
this form (the
accept-charset
attribute). User agents may advise the
user of the value of the
accept-charset
attribute and/or
restrict the user's ability to enter unrecognized characters.
A form can contain text and markup (paragraphs, lists, etc.) in addition to
form controls.
The following example shows a form that is to be processed by the "adduser"
program when submitted. The form will be sent to the program using the HTTP
"post" method.

...form contents...

Please consult the section on
form submission
for information about how user agents must prepare form data for servers and
how user agents should handle expected responses.
Note.
Further discussion on the behavior of servers
that receive form data is beyond the scope of this specification.
17.4
The
INPUT
element
"(TEXT | PASSWORD | CHECKBOX |
RADIO | SUBMIT | RESET |
FILE | HIDDEN | IMAGE | BUTTON)"


INPUT
- O EMPTY -- form control -->
%attrs;
--
%coreattrs
%i18n
%events
--
type
%InputType;
TEXT -- what kind of widget is needed --
name
CDATA
#IMPLIED -- submit as part of form --
value
CDATA
#IMPLIED -- Specify for radio buttons and checkboxes --
checked
(checked) #IMPLIED -- for radio buttons and check boxes --
disabled
(disabled) #IMPLIED -- unavailable in this context --
readonly
(readonly) #IMPLIED -- for text and passwd --
size
CDATA
#IMPLIED -- specific to each type of field --
maxlength
NUMBER
#IMPLIED -- max chars for text fields --
src
%URI;
#IMPLIED -- for fields with images --
alt
CDATA
#IMPLIED -- short description --
usemap
%URI;
#IMPLIED -- use client-side image map --
ismap
(ismap) #IMPLIED -- use server-side image map --
tabindex
NUMBER
#IMPLIED -- position in tabbing order --
accesskey
%Character;
#IMPLIED -- accessibility key character --
onfocus
%Script;
#IMPLIED -- the element got the focus --
onblur
%Script;
#IMPLIED -- the element lost the focus --
onselect
%Script;
#IMPLIED -- some text was selected --
onchange
%Script;
#IMPLIED -- the element value was changed --
accept
%ContentTypes;
#IMPLIED -- list of MIME types for file upload --
Start tag:
required
, End tag:
forbidden
Attribute definitions
type
text|password|checkbox|radio|submit|reset|file|hidden|image|button
[CI]
This attribute specifies the
type of
control
to create. The default value for this attribute is "text".
name
cdata
[CI]
This attribute assigns the
control name
value
cdata
[CA]
This attribute specifies the
initial value
of
the control. It is optional except when the
type
attribute has the value "radio" or "checkbox".
size
cdata
[CN]
This attribute tells the user agent the initial width of the control. The
width is given in
pixels
except when
type
attribute has the value "text" or "password". In that case, its
value refers to the (integer) number of characters.
maxlength
number
[CN]
When the
type
attribute has the value "text" or "password",
this attribute specifies the maximum number of characters the user may enter.
This number may exceed the specified
size
, in which case the
user agent should offer a scrolling mechanism. The default value for this
attribute is an unlimited number.
checked
[CI]
When the
type
attribute has the value "radio" or "checkbox",
this boolean attribute specifies that the button is on. User agents must ignore
this attribute for other control types.
src
uri
[CT]
When the
type
attribute has the value "image", this attribute
specifies the location of the image to be used to decorate the graphical submit
button.
Attributes defined elsewhere
id
class
document-wide identifiers
lang
language information
),
dir
text
direction
title
element
title
style
inline
style information
alt
alternate text
align
alignment
accept
legal content types for a
server
readonly
read-only input controls
disabled
disabled input controls
tabindex
tabbing navigation
accesskey
access
keys
usemap
client-side image maps
ismap
server-side image maps
onfocus
onblur
onselect
onchange
onclick
ondblclick
onmousedown
onmouseup
onmouseover
onmousemove
onmouseout
onkeypress
onkeydown
onkeyup
intrinsic events
17.4.1
Control types
created with INPUT
The
control type
defined by the
INPUT
element depends on the value of the
type
attribute:
text
Creates a
single-line
text
input
control.
password
Like "text",
but the input text is
rendered in such a way as to hide the characters (e.g., a series of asterisks).
This control type is often used for sensitive input such as passwords. Note
that the
current value
is the text
entered
by the user, not the text rendered by the user agent.
Note.
Application designers should
note that this mechanism affords only light security protection. Although the
password is masked by user agents from casual observers, it is transmitted to
the server in clear text, and may be read by anyone with low-level access to
the network.
checkbox
Creates a
checkbox.
radio
Creates a
radio button.
submit
Creates a
submit button.
image
Creates a graphical
submit button.
The value
of the
src
attribute specifies the URI of the image that will decorate the
button. For accessibility reasons, authors should provide
alternate text
for the image via
the
alt
attribute.
When a pointing device is used to click on the
image,
the form is submitted and the click coordinates passed to the
server. The x value is measured in
pixels
from the left of the image, and the y value in
pixels
from the top of the image. The submitted
data includes
name
.x=
x-value
and
name
.y=
y-value
where
"name"
is the value of the
name
attribute, and
x-value
and
y-value
are the x and y coordinate values, respectively.
If the server takes different actions depending on the location clicked,
users of non-graphical browsers will be disadvantaged. For this reason, authors
should consider alternate approaches:
Use multiple submit buttons (each with its own image) in place of a single
graphical submit button. Authors may use style sheets to control the
positioning of these buttons.
Use a
client-side image
map
together with scripting.
reset
Creates a
reset button.
button
Creates a
push button.
User agents should use
the value of the
value
attribute as the button's
label.
hidden
Creates a
hidden control.
file
Creates a
file select
control. User agents may
use the value of the
value
attribute as the initial file name.
17.4.2
Examples of forms containing INPUT
controls
The following sample HTML fragment defines a simple form that allows the
user to enter a first name, last name, email address, and gender. When the
submit button is activated, the form will be sent to the program specified by
the
action
attribute.



First name:

Last name:

email:

Male

Female




This form might be rendered as follows:
In the section on the
LABEL
element, we discuss marking up labels such as
"First name".
In this next example, the JavaScript function name
verify
is
triggered when the "onclick" event occurs:









Please consult the section on
intrinsic
events
for more information about scripting and events.
The following example shows how the contents of a user-specified file may be
submitted with a form. The user is prompted for his or her name and a list of
file names whose contents should be submitted with the form. By specifying the
enctype
value of "multipart/form-data", each file's contents will be
packaged for submission in a separate section of a multipart document.
enctype="multipart/form-data"
method="post">


What is your name?
What files are you sending?



17.5
The
BUTTON
element
BUTTON
- -
%flow;
)* -(A|
%formctrl;
|FORM|FIELDSET)
-- push button -->
%attrs;
--
%coreattrs
%i18n
%events
--
name
CDATA
#IMPLIED
value
CDATA
#IMPLIED -- sent to server when submitted --
type
(button|submit|reset) submit -- for use as form button --
disabled
(disabled) #IMPLIED -- unavailable in this context --
tabindex
NUMBER
#IMPLIED -- position in tabbing order --
accesskey
%Character;
#IMPLIED -- accessibility key character --
onfocus
%Script;
#IMPLIED -- the element got the focus --
onblur
%Script;
#IMPLIED -- the element lost the focus --
Start tag:
required
, End tag:
required
Attribute definitions
name
cdata
[CI]
This attribute assigns the
control name.
value
cdata
[CS]
This attribute assigns the
initial value
to
the button.
type
submit|button|reset
[CI]
This attribute declares the type of the button. Possible values:
submit:
Creates a
submit button.
This is the default value.
reset:
Creates a
reset
button.
button:
Creates a
push button.
Attributes defined elsewhere
id
class
document-wide identifiers
lang
language information
),
dir
text
direction
title
element
title
style
inline
style information
disabled
disabled input controls
accesskey
access
keys
tabindex
tabbing navigation
onfocus
onblur
onclick
ondblclick
onmousedown
onmouseup
onmouseover
onmousemove
onmouseout
onkeypress
onkeydown
onkeyup
intrinsic events
Buttons created with the
BUTTON
element function just like buttons
created with the
INPUT
element, but they offer richer rendering
possibilities: the
BUTTON
element may have content. For example, a
BUTTON
element that contains an image functions like and may resemble an
INPUT
element whose
type
is set to "image", but the
BUTTON
element type allows content.
Visual user agents may render
BUTTON
buttons with relief and an
up/down motion when clicked, while they may render
INPUT
buttons as "flat" images.
The following example expands a previous example, but creates
submit
and
reset
buttons with
BUTTON
instead of
INPUT
. The buttons contain images by way of the
IMG
element.


First name:

Last name:

email:

Male

Female





Recall that authors must provide
alternate text
for an
IMG
element.
It is illegal
to
associate an image map with an
IMG
that appears as the contents of a
BUTTON
element.
ILLEGAL EXAMPLE:
The following is not legal HTML.

17.6
The
SELECT
OPTGROUP
, and
OPTION
elements
SELECT
- - (OPTGROUP|OPTION)+ -- option selector -->
%attrs;
--
%coreattrs
%i18n
%events
--
name
CDATA
#IMPLIED -- field name --
size
NUMBER
#IMPLIED -- rows visible --
multiple
(multiple) #IMPLIED -- default is single selection --
disabled
(disabled) #IMPLIED -- unavailable in this context --
tabindex
NUMBER
#IMPLIED -- position in tabbing order --
onfocus
%Script;
#IMPLIED -- the element got the focus --
onblur
%Script;
#IMPLIED -- the element lost the focus --
onchange
%Script;
#IMPLIED -- the element value was changed --
Start tag:
required
, End tag:
required
SELECT Attribute definitions
name
cdata
[CI]
This attribute assigns the
control name.
size
number
[CN]
If a
SELECT
element is presented as a scrolled list box, this attribute
specifies the number of rows in the list that should be visible at the same
time. Visual user agents are not required to present a
SELECT
element as a list box; they may use any other mechanism, such as a drop-down
menu.
multiple
[CI]
If set, this boolean attribute allows multiple selections. If not set, the
SELECT
element only permits single selections.
Attributes defined elsewhere
id
class
document-wide identifiers
lang
language information
),
dir
text
direction
title
element
title
style
inline
style information
disabled
disabled input controls
tabindex
tabbing navigation
onclick
ondblclick
onmousedown
onmouseup
onmouseover
onmousemove
onmouseout
onkeypress
onkeydown
onkeyup
intrinsic events
The
SELECT
element creates a
. Each choice
offered by the menu is represented by an
OPTION
element. A
SELECT
element must contain at least one
OPTION
element.
The
OPTGROUP
element
allows authors to group choices
logically. This is particularly helpful when the user must choose from a long
list of options; groups of related choices are easier to grasp and remember
than a single long list of options. In HTML 4, all
OPTGROUP
elements must be specified directly within a
SELECT
element (i.e., groups may not be nested).
17.6.1
Pre-selected
options
Zero or more choices may be pre-selected for the user. User agents should
determine which choices are pre-selected as follows:
If no
OPTION
element has the
selected
attribute set, user agent
behavior for choosing which option is initially selected is undefined.
Note.
Since existing implementations handle this case differently, the
current specification differs from RFC 1866 (
[RFC1866]
section 8.1.3),
which states:
The initial state has the first option selected, unless a SELECTED
attribute is present on any of the