Go to main content
Bypass navigation bar HTML Reference HTML Tags HTML Tutorials
Bypass language selection
Bypass location bar

Location: Home > HTML Reference > HTML Tags > HTML form tag

Bypass main content

HTML form tag

Note: If you don't know what an element/tag is and/or how you must use it we recommend you read our HTML tags and attributes tutorial, which you can find in our HTML tutorials section.

Bypass main content

Description

The HTML form element inserts a form in the document. A form, is a method to allow authors to collect information from the visitors, and can provide a set of controls for every need (see more at the "Forms in HTML" tutorial).

When a visitor reaches the form, he only needs to fill the fields and send the form, usually with a submit button. The sent form is received and processed by a processing agent (usually a server side script) specified in the value of the "action" attribute of the form element.

In XHTML 1.0 the "name" attribute for this element has been deprecated in favor of the "id" attribute. Its use is no longer recommended.

Examples

The following example shows an example form.

Code
<form action="example.php">
<fieldset>
<legend>Personal information</legend>
Name: <input type="text" name="pname" />
Address: <input type="text" name="paddress" />
Phone: <input type="text" name="pphone" />
</fieldset>
<fieldset>
<legend>Work information</legend>
Address: <input type="text" name="waddress" />
Phone: <input type="text" name="wphone" />
</fieldset>
</form>
View
Personal information Name:
Address:
Phone:
Work information Address:
Phone:

Attributes

id (name)

The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element in other instances (e.g., from client-side scripts).

<p id="paragraph1">This is the first paragraph, named "paragraph1". To dynamically change its properties use this identifier.</p>

class (cdata)

The "class" attribute assigns a class name (or a list of class names separated by spaces) to the container element. It is used together with style sheets and tells the browser the class (or classes) to which the element is associated.

A class gives presentational attributes to elements (read more at the Cascading Style Sheets tutorial).

<p class="references">This article is based on the book "Wind in the trees" by Jhon L. Brooks</p>
<p class="references important">This article is based on the book "Wind in the trees" by Jhon L. Brooks... and is more important than the one before.</p>

style (style)

This attribute is used to define presentational attributes for the containing element, and its value should be composed by style sheets properties. Although in some cases it can become useful, a better practice is to place presentational attributes in external files, relating them to elements with the "class" attribute. This way you keep the semantic and presentational parts of your document separated.

You can find more information about presentational attributes at the Cascading Style Sheets tutorial.

<p style="color: #0000FF; font-size: 12pt">This is a paragraph with a defined style.</p>
<p>And this is another text without style.</p>

title (text)

The purpose of this attribute is to provide a title for the element. Its value must be a short and accurate description of the element. Browsers usually render it as a "tool tip" when the user puts the mouse pointer over the element for a short period of time.

Code
<a title="HTMLQuick.com" href="http://www.htmlquick.com">HTML code</a>
View

lang (langcode)

Specifies the language of an element's content. The default value is "unknown".

When writing XHTML 1.0 documents, the attribute used to specify the language of an elements is "xml:lang". For forward and backward compatibility both attributes can be used simultaneously as in the example below. Note, that in XHTML 1.1 the "lang" attribute has been completely replaced by "xml:lang" and its use is no longer valid.

<p lang="en" xml:lang="en">This is a paragraph in English.</p>
<p lang="es" xml:lang="es">Este es un párrafo en español.</p>

dir

This attribute indicates the direction in which the texts of the element must be read. This includes content, attribute values and tables. It has two possible values that are case-insensitive:

  • RTL: Right to left.
  • LTR: Left to right.
<q lang="he" dir="rtl">...an Hebrew quotation...</q>

action (uri)

Points to a file that acts as processing agent for the form data. This agent will process the information as needed (compose an e-mail, save the data into a database, etc.).

If this attribute is not present, the form won't be sent.

<form action="../save-user-info.php">
...Form fields...
</form>

method

The "method" attribute establishes how the form's data will be sent to the processing agent. There are two possible values for this attribute (case-insensitive):

  • get: The form's data is added to the URI defined in the action attribute (e.g., "handler.php?pname=jhon&plastname=malcovich").
  • post: The form's data is added to the body of the form.
<form action="../save-user-info.php" method="post">
...Form fields...
</form>

enctype (content-type)

Specifies the content type of the submitted data, when the value of the "method" attribute is "post". For example, the value "multipart/form-data" is used when the form allows users to upload files.

The default value for this attribute is "application/x-www-form-urlencoded".

<form action="../save-user-info.php" method="post" enctype="multipart/form-data">
...Form fields with file upload...
</form>

accept (content-type)

Defines a space and/or comma separated list of content types that the processing agent is suposed to handle correctly.

This could be useful when uploading files, for example, to filter files on the client side, that the processing agent won't be able to handle (e.g., only allow JPG and GIF images).

<form action="../save-user-info.php" method="post" enctype="multipart/form-data" accept="image/gif,image/jpg">
...Form fields...
</form>

accept-charset (charset)

Defines a space and/or comma separated list of charsets that the processing agent is suposed to handle correctly.

<form action="../save-user-info.php" accept-charset="utf-8,iso-8859-1">
...Form fields...
</form>

name (cdata)

Assigns a name to the element for future reference.

In XHTML 1.0 the "name" attribute for this element has been deprecated in favor of the "id" attribute and in XHTML 1.1 it's simply invalid. Therefore, its use is no longer recommended.

<form name="contact-form">
...Form fields...
</fomr>

Events

  • onsubmit
  • onreset
  • onclick
  • ondblclick
  • onmousedown
  • onmouseup
  • onmouseover
  • onmousemove
  • onmouseout
  • onkeypress
  • onkeydown
  • onkeyup

See a complete list and information about events in HTML

Diseño y desarrollo: Latitud29.com

Links and logos|Contact|Beyond HTML|Tools and resources|Sitemap|Webmaster|Donate