Definition and Usage
The <form> tag represents an area of a web form for submitting data.
This area contains control elements related to web forms and is used to define a form for submitting the data from these elements.
- Control Elements Related to Web Forms
- The term commonly used is 'form controls'.
These refer to elements that provide controls for users to interact with web pages and are used together with form elements.<input>,<textarea>,<select>,<button>, and others are elements that allow users to enter, select, and submit data.
Basic Example
The example below demonstrates one of the common uses of the <form> tag.
<form action="submit_url" method="get" class="form-example">
<div class="form-example">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name" required>
</div>
<div class="form-example">
<label for="user-email">Email</label>
<input type="email" id="user-email" name="user-email" required>
</div>
<button type="submit">Submit</button>
</form>
Key Technical Syntax Summary
You cannot nest a <form> tag as a descendant of another <form> tag.
Includes Form-Related Control Elements
The <form> tag is an area that contains form-related control elements for submitting data.
It can contain elements such as text input fields, checkboxes, radio buttons, drop-down lists, and various types of buttons, and is used to define a form for submitting the data from these elements.
Reference: Key Form-Related Control Elements
<input>
|
Represents an interactive control for receiving data from the user |
|---|---|
<textarea>
|
Represents a multi-line text input field |
<select>
|
Represents a control for selecting an option from a set of available options |
<button>
|
Represents a button labeled by its contents |
The <form> tag can contain not only these form-related control elements, but also most content that forms the structure of a document (referred to as flow content), such as paragraphs, headings, lists, tables, images, and videos.
Using Related Attributes
Let's learn about the attributes related to the <form> tag.
action Attribute
The action attribute specifies the URL to which the data from the control elements contained in the form area defined by the <form> tag is sent when the form is submitted.
How It Works
If there is a button related to form submission within the <form>, such as <input type="submit">, <input type="image">, or <button type="submit">, the data is sent to the URL specified in the action attribute when the button is clicked.
<form action="submit_url">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
<button type="submit">Submit</button>
</form>
Note!
If there is no button related to form submission within the <form>, you can also submit the form area by using JavaScript to call the submit() method. In this case as well, the data is sent to the URL specified in the action attribute. In addition, you can use a submit event handler to send the data to a desired URL.
If the action Attribute Is Not Specified or Is Set to an Empty String ("")
The data is sent to the current URL.
Note!
Even if the action attribute is not specified or is set to an empty string (""), when the form area is submitted, you can use JavaScript, such as a submit event handler, to send the data to a desired URL.
method Attribute
Specifies the method for submitting the data from the form area when the form is submitted.
Values
The value of this attribute is the HTTP method to use for submitting the data from the form area when the form is submitted. It is case-insensitive.
get |
The default value when the method attribute is not specified.
Sends the data as a query string in the URL |
|---|---|
post |
Sends the data in the body of the HTTP request |
dialog |
When the form area is inside a <dialog>, closes the dialog without sending the data or resetting the form, and triggers the submit event when submitted |
<form action="submit_url" method="post">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
<button type="submit">Submit</button>
</form>
novalidate Attribute
Indicates that the form area should not be validated when the form is submitted. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
There are two types of validation for the form area.
- Validation provided as a built-in feature of HTML5
- Checking whether a value has been specified for controls with the
requiredattribute
Validation Provided as a Built-in Feature of HTML5
<input type="url"> and <input type="email"> are automatically validated by the browser by default when used within a <form> tag.
This is a built-in feature of HTML5. For <input type="url">, the browser automatically checks whether the user has entered a URL in the correct format, while for <input type="email">, it automatically checks whether the user has entered an email address in the correct format.
To disable the validation provided as a built-in feature of HTML5, add the novalidate attribute to the <form> tag.
The following example compares a <form> tag without the novalidate attribute and one with the novalidate attribute.
<h3>Validation Is Performed</h3>
<p>Checks whether a URL is entered in the correct format</p>
<form>
<label for="user-url">Website URL</label>
<input type="url" id="user-url" name="user-url">
<button type="submit">Submit</button>
</form>
<h3>Validation Is Not Performed</h3>
<p>Does not check whether a URL is entered in the correct format</p>
<form novalidate>
<label for="user-url-1">Website URL</label>
<input type="url" id="user-url-1" name="user-url-1">
<button type="submit">Submit</button>
</form>
Note! If you do not enter a URL, the form is submitted without validation.
Validation Is Performed
Checks whether a URL is entered in the correct format
Validation Is Not Performed
Does not check whether a URL is entered in the correct format
Checking Whether a Value Has Been Specified for Controls with the required Attribute
The required attribute indicates that an element is a required control element for which the user must enter or select a value in advance before submitting the form.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
The following example compares a <form> tag without the novalidate attribute and one with the novalidate attribute.
<h3>Checks for the <code>required</code> Attribute</h3>
<p>Checks whether the <code>required</code> attribute is present</p>
<form>
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name" required>
<button type="submit">Submit</button>
</form>
<h3>Does Not Check for the <code>required</code> Attribute</h3>
<p>Does not check whether the <code>required</code> attribute is present</p>
<form novalidate>
<label for="user-name-1">Name</label>
<input type="text" id="user-name-1" name="user-name-1" required>
<button type="submit">Submit</button>
</form>
required attribute is checked!
Checks for the required Attribute
Checks whether the required attribute is present
Does Not Check for the required Attribute
Does not check whether the required attribute is present
target Attribute
Specifies where to display the result after the form is submitted. It is mainly used to specify the target frame or window of a link or form.
Values
_self |
The default value when the target attribute is not specified.
The form submission result is displayed in the current window or frame |
|---|---|
_blank |
The form submission result is displayed in a new window or tab |
_parent |
The form submission result is displayed in the parent frame. If there is no frame structure, it behaves the same as _self |
_top |
The form submission result is displayed in the top-level frame of the entire window. If there is no frame structure, it behaves the same as _self |
<form action="submit_url" method="post" target="_blank">
<label for="user-name">Name</label>
<input type="text" id="user-name" name="user-name">
<button type="submit">Submit</button>
</form>
enctype Attribute
Specifies the encoding type used when the data from the control elements contained in the form area is submitted to the server. This attribute is mainly used for forms whose method attribute is set to post. There are several main options for the value of the enctype attribute.
Values
application/x-www-form-urlencoded |
The default value when the enctype attribute is not specified.
All characters are URL-encoded and sent. This is the basic form data encoding method. |
|---|---|
multipart/form-data |
Supports file uploads.
The form data is divided into multiple parts, and each part is encoded separately. Used when <input type="file"> is included. |
text/plain |
The form data is encoded as plain text. |
<form action="submit_url" method="post" enctype="multipart/form-data">
<label for="user-file">File Upload</label>
<input type="file" id="user-file" name="user-file">
<button type="submit">Submit</button>
</form>
name Attribute
The name attribute specifies the name to use when referencing the form in JavaScript.
When there are multiple <form> tags in a document, this name is used to identify the corresponding element in JavaScript.
Technical Summary
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<form>
|
1 | 12 | 1 | 4 |
action
|
1 | 12 | 1 | 3 |
novalidate
|
10 | 12 | 4 | 10.1 |
target
|
1 | 12 | 1 | 3 |
enctype
|
1 | 12 | 1 | 3 |
name
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
<form>
|
HTML Standard #the-form-element |