HTML form tag
Note: If you don't know what a tag is and/or how you must use it we recommend you to read our HTML tags and attributes tutorial that you can find in our HTML tutorials section.
Description
The
HTML form tag acts as a container for controls. Forms are used to collect and process information inputted by the users, through its controls. The attributes of the HTML form tag defines significant information about how and who will process its data, while the controls inside of it take care of the data input. A form may have many controls that can be grouped thematically with the
HTML fieldset tag.
Attributes
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.
Example:
Code begin
<p id="paragraph1">This is the first paragraph named as paragraph1. To dynamically change its properties use this identifier.</p>Code end
The "class" attribute assigns a class name (or a list of class names separated by spaces) to the container element. It's used with style sheets and tells the browser the class to which the element is associated with. A class gives visual attributes to elements.
Example:
Code begin
<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>Code end
Defines a visual style of this element. Is a better practice to define styles attributes in external style sheets grouping them in classes. Attributes in the "style" parameter must preserve this order "name : value" and be separated by a semi-colon.
If you're writing
XHTML code it's recommended not to use this attribute and try style sheet classes (with the "class" attribute).
Example:
Code begin
<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>Code end
Indicates a title for the element. Used to give a short description about the element that is usually shown as a "tool tip" when the user put the mouse pointer over the element.
Example:
Code |
View |
<a title="HTMLQuick.com" href="https://www.htmlquick.com">HTML code</a> |
HTML code |
Specifies the language of an element's content. The default value in "unknown".
When writing
XHTML code the syntax "xml:lang" represents a preferred alternative in XHTML 1.0 and a replacement in XHTML 1.1 (e.g., xml:lang="en").
Example:
Code begin
<p lang="en">This is a paragraph in english.</p>
<p lang="es">Este es un párrafo en español.</p>Code end
dir
Specifies the text direction of the element's contents and attribute values, as well as tables directionality. It has two possible values that are case insensitive:
- RTL: Right to left.
- LTR: Left to right.
Example:
Code begin
<q lang="he" dir="rtl">...a Hebrew quotation...</q>Code end
Specifies the name of the frame where the destination document should be loaded. Refer to the
frame-target type definition for further information.
Code begin
Open in a new window: <a href="https://www.w3c.org" target="_blank">WWW Consortium</a>
Open in a frame named "content" (frame must be present in the actual frameset): <a href="https://www.w3c.org" target="content">WWW Consortium</a>Code end
action (uri)
Designates the location of the file that will handle the form's inputs. Forms are usually handled by server side scripts.
Example:
Code begin
<form action="handler.php">Code end
method
The "method" attribute define how form's data is 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.
Specifies the conten-type of the submitted data, when the value of the "method" attribute is "post". For example, the value "multipart/form-data" should be used when uploading files through a form. Default value is "application/x-www-form-urlencoded".
accept-charset (charset)
Defines a list, space and/or comma separated, of charsets. This list should contain the character encodings that the processing agent will be able to handle.
Example:
Code begin
<form accept-charset="iso-8859-1,UTF-8">Code end
accept
Defines a comma separated list of content-types that the processing agent will handle correctly. This may be used to filter files for upload in the client side (e.g., only allow to upload compressed images, like JPG and GIF).
Example:
Code begin
<form accept="image/gif,image/jpg">Code end
Assigns a control name to the element.
Events
- onmousedown
- onreset
- onclick
- ondblclick
- onmousedown
- onmouseup
- onmouseover
- onmousemove
- onmouseout
- onkeypress
- onkeydown
- onkeyup
See complete list and information about
events in HTML
Examples
A simple example of a form used to validate a user info.
Code |
View |
<form id="authform" action="authuser.asp" method="post"> <div><fieldset> <legend>User info</legend> Username: <input type="text" name="user" value="-" /><br /> Password: <input type="password" name="pass" value="-" /> </fieldset> <input type="button" value="Enter" /> </div> </form> |
|
A form used to upload a picture. The formats allowed are GIF and JPEG.
Code |
View |
<form id="pictureform" action="savepicture.php" method="post" enctype="multipart/form-data" accept="image/gif,image/jpg"> <div> Picture: <input type="file" name="picture" value="-" /><br /> <input type="button" value="Send" /> </div> </form> |
|