tadam logo

HTML <form> Tag

Example

A simple HTML form with two input fields and one submit button:
<form action="http://xhtml.co.il/tryit/html/html_form_submit_736.php">
First name: <input type="text" name="fname" value="Mickey" />
Last name: <input type="text" name="lname" value="Mouse" />
<input type="submit" value="Submit" />
</form>
The output of the code above will be:

Definition and Usage

The <form> tag is used to create an HTML form for user input.

A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select menus, textarea, fieldset, legend, and label elements.

Forms are used to pass data to a server.

Browser Support

The <form> tag is supported in all major browsers

Differences Between HTML and XHTML

NONE

Tips and Notes

Note: The form element is a block-level element, and creates a line break before and after itself.

Required Attributes

DTD indicates in which HTML 4.01/XHTML 1.0 DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.
AttributeValueDescriptionDTD
actionURLSpecifies where to send the form-data when a form is submittedSTF

Optional Attributes

AttributeValueDescriptionDTD
acceptMIME_typeSpecifies the types of files that can be submitted through a file uploadSTF
accept-charsetcharsetSpecifies the character-sets the server can handle for form-dataSTF
enctypeapplication/x-www-form-urlencoded
multipart/form-data
text/plain
Specifies how form-data should be encoded before sending it to a serverSTF
methodget
post
Specifies how to send form-dataSTF
namenameSpecifies the name for a formTF
target_blank
_self
_parent
_top
framename
Deprecated. Specifies where to open the action URLTF

Standard Attributes

Тег <form> поддерживает следующие стандартные атрибуты:
AttributeValueDescriptionDTD
classclassnameSpecifies a classname for an elementSTF
dirrtl
ltr
Specifies the text direction for the content in an elementSTF
ididSpecifies a unique id for an elementSTF
langlanguage_codeSpecifies a language code for the content in an elementSTF
stylestyle_definitionSpecifies an inline style for an elementSTF
titletextSpecifies extra information about an elementSTF
xml:langlanguage_codeSpecifies a language code for the content in an element, in XHTML documentsSTF
More information about Standard Attributes.

Event Attributes

The <form> tag supports the following event attributes:
AttributeValueDescriptionDTD
onclickscriptScript to be run on a mouse clickSTF
ondblclickscriptScript to be run on a mouse double-clickSTF
onmousedownscriptScript to be run when mouse button is pressedSTF
onmousemovescriptScript to be run when mouse pointer movesSTF
onmouseoutscriptScript to be run when mouse pointer moves out of an elementSTF
onmouseoverscriptScript to be run when mouse pointer moves over an elementSTF
onmouseupscriptScript to be run when mouse button is releasedSTF
onkeydownscriptScript to be run when a key is pressedSTF
onkeypressscriptScript to be run when a key is pressed and releasedSTF
onkeyupscriptScript to be run when a key is releasedSTF
onresetscriptScript to be run when a form is resetSTF
onsubmitscriptScript to be run when a form is submittedSTF

More information about Event Attributes.

More examples

Example 1

Form with checkboxes: A form with two checkboxes, and a submit button.
<form action="http://xhtml.co.il/tryit/html/checkboxes.php" method="get">
  <input type="checkbox" name="vehicle[]" value="Bike" /> I have a bike
  <input type="checkbox" name="vehicle[]" value="Car" checked="checked" /> I have a car
  <input type="submit" value="Submit" />
</form>
The output of the code above will be:

Example 2

Form with radiobuttons: A form with two radio buttons, and a submit button.
<form action="http://xhtml.co.il/tryit/html/radio.php" method="get">
  <input type="radio" name="sex" value="Male" /> Male
  <input type="radio" name="sex" value="Female" checked="checked" /> Female
  <input type="submit" value="Submit" />
</form>
The output of the code above will be:

Example 3

Send e-mail from a form: How to send e-mail from a form.
<form action="mailto:someone@example.com" method="post" enctype="text/plain">
Name: <input type="text" name="name" size="20" />
Email: <input type="text" name="email" size="20" />
Comment: <input type="text" name="comment" size="40" />
<input type="submit" value="Send" />
<input type="reset" value="Reset" />
</form>
The output of the code above will be:
Was this information helpful?
   

Comments