The Web Design Group

form - Interactive Form

Syntax <form>...</form>
Attribute Specifications
  • action=URI (form handler)
  • method=[ "get" | "post" ] (HTTP method for submitting form)
  • enctype=ContentType (content type to submit form as)
  • accept-charset=Charsets (supported character encodings)
  • target=FrameTarget (frame to render form result in)
  • accept=ContentTypes (media types for file upload)
  • name=Text (element identifier)
  • onsubmit=Script (form was submitted)
  • onreset=Script (form was reset)
  • common attributes
Contained in applet, blockquote, body, center, dd, del, div, fieldset, iframe, ins, li, map, noframes, noscript, object, td, th

The form element defines an interactive form. The element should contain form controls--input, select, textarea, and button--through which the user interacts.

When the user submits the form, through an input or button element with type="submit", the form values are submitted to the URI given in form's required action attribute. action usually points to a CGI script or Java servlet that handles the form submission.

A mailto URI (e.g., is also allowed as an action, but this is not supported by all browsers. Non-supporting browsers such as Microsoft Internet Explorer 3.x typically will open a blank e-mail message when the user submits a mailto form. Even on supporting browsers, mailto forms are troublesome in that they fail to provide feedback to the user after the form submission.

Free CGI scripts exist for handling forms; some are even remotely hosted for authors whose providers refuse to allow CGI scripts to be run locally.

How the form input is sent to the server depends on the method and enctype attributes. When the method is get (the default), the form input is submitted as an HTTP GET request with ?form_data appended to the URI specified in the action attribute.

Using the get method allows the form submission to be contained completely in a URL. This can be advantageous in that it permits bookmarking in current browsers, but it also prevents form data from containing non-ASCII characters such as "é" and "©". As well, the amount of form data that can be handled by the get method is limited by the maximum length of the URL that the server and browser can process. To be safe, any form whose input might contain non-ASCII characters or more than 100 characters should use method="post".

With a method value of post, the form input is submitted as an HTTP POST request with the form data sent in the body of the request. Most current browsers are unable to bookmark POST requests, but POST does not entail the character encoding and length restrictions imposed by GET.

The enctype attribute specifies the content type used in submitting the form, and defaults to application/x-www-form-urlencoded. This content type results in name/value pairs sent to the server as name1=value1&name2=value2... with space characters replaced by "+" and reserved characters (like "#") replaced by "%HH" where HH is the ASCII code of the character in hexadecimal. Line breaks are encoded as "%0D%0A"--a carriage return followed by a line feed.

Authors should generally only use a different enctype when the form includes a type="file" input element, in which case the enctype should be multipart/form-data and the method must be post. The format of multipart/form-data requests is given in RFC 1867.

Tools such as cg-eye allow authors to easily create and view a request, simulating the submission of a form. However, authors often do not need to concern themselves with the exact format of the submission; CGI libraries including transparently handle get and post submissions sent as application/x-www-form-urlencoded or multipart/form-data.

The accept-charset attribute specifies a list of character encodings that are accepted by the form handler. The value consists of a list of "charsets" separated by commas and/or spaces. The default value is unknown and is usually considered to be the character encoding used to transmit the document containing the form.

The target attribute is used with frames to specify in which frame the form response should be rendered. If no frame with such a name exists, the response is rendered in a new window unless overridden by the user. Special frame names begin with an underscore:

In XHTML 1.0, the target attribute value is case-insensitive, so that _top and _TOP both have the same meaning. However, most browsers treat the target attribute value as case-sensitive and do not recognize _TOP as having the special meaning of _top.

The accept attribute gives a comma-separated list of media types accepted, allowing the browser to filter out inappropriate files. Current browsers generally ignore the accept attribute.

The name attribute gives an identifier, similar to the id attribute and is included for compatibility with older browsers.

The form element also takes a number of attributes to specify client-side scripting actions for various events. In addition to the core events common to most elements, input accepts the following event attributes:

More Information