HTML Reference Β· Forms
The <form> element
Reference for the HTML <form> element: action, method (get, post, dialog), enctype, target, novalidate, autocomplete, name, rel and accept-charset, with examples.
The <form> element is a container for form controls whose values are collected and sent together. When a form is submitted, the browser takes every named, enabled control, builds a list of name=value pairs and sends them to the URL in action using the HTTP method in method.
<form action="/subscribe" method="post"> <label for="email">Email</label> <input type="email" id="email" name="email" required> <button>Subscribe</button> </form>
Try it
Previews on this site can't send data to a server, so the example cancels the real submission and prints what the browser would have sent.
A method="dialog" form sends nothing over the network: submitting it closes the dialog and stores the clicked button's value in dialog.returnValue.
Attributes
| Attribute | Values | Description |
|---|---|---|
action |
URL | Where the data is sent. Absolute, root-relative or relative URLs all work. Defaults to the current page's URL. |
method |
get (default), post, dialog |
How the data is sent. get appends it to the URL as a query string; post sends it in the request body; dialog closes the surrounding <dialog> without sending anything. |
enctype |
application/x-www-form-urlencoded (default), multipart/form-data, text/plain |
How a post body is encoded. Use multipart/form-data when the form contains a file input. text/plain is meant for debugging only. |
target |
_self (default), _blank, _parent, _top, or a browsing-context name |
Where to show the response: the same tab, a new tab, a parent or top-level frame, or a named window or <iframe>. |
novalidate |
Boolean | Turns off the browser's built-in constraint validation when the form is submitted. |
autocomplete |
on (default), off |
Default autofill behavior for the form's controls. Individual controls can override it with their own autocomplete. |
name |
Text | A name for scripts: the form is available as document.forms.name. Must be unique among the document's forms and not empty. |
rel |
Space-separated link types, such as noopener, noreferrer, opener, nofollow, external, help, license, next, prev, search |
The relationship of the submission's target, like rel on links. noreferrer stops the browser from sending the Referer header. |
accept-charset |
UTF-8 |
Character encoding used for the submission. The only valid value is UTF-8, which is also what you get by default on a UTF-8 page, so you rarely need it. |
It also accepts the global attributes. Submit buttons can override action, method, enctype, target and novalidate for their own submissions with formaction, formmethod, formenctype, formtarget and formnovalidate; see <button>.
Usage notes
- Only named controls are sent. A control without a
name, a disabled control, an unchecked checkbox or radio, and a<button>that wasn't clicked are all left out. - GET for reading, POST for changing. Use
getfor searches and filters: the results can be bookmarked and shared. Usepostfor anything that creates, updates or deletes data, sends personal details, or uploads files. - File uploads need two things:
method="post"andenctype="multipart/form-data". Otherwise only the file names are sent. - Pressing Enter submits. In a single-line text field, Enter triggers implicit submission through the form's first submit button.
- No nested forms. A form can't contain another form. To place a control outside its form, give the control a
form="form-id"attribute instead. novalidateis not security. Browser validation is a convenience; always validate again on the server.- Accessibility. A form only becomes a
formlandmark when it has an accessible name (for examplearia-labeloraria-labelledby). Give every control a<label>, and group related controls with<fieldset>. - Testing safely. While developing, a submit handler that calls
event.preventDefault()and logsnew FormData(form)shows exactly what would be sent.
Technical summary
| Content categories | Flow content, palpable content |
| Permitted content | Flow content, but no <form> element descendants |
| Tag omission | None; both the start and end tags are required |
| Permitted parents | Any element that accepts flow content |
| Implicit ARIA role | form (exposed as a landmark only when the form has an accessible name) |
| DOM interface | HTMLFormElement |
| Default display | block |
Browser support
Supported in every browser. method="dialog" works in all current browsers.
Related
<input>and<input>types β the most common controls<button>β submitting and per-button overrides<label>and<fieldset>β naming and grouping controls- HTTP methods β what GET and POST mean
- Lesson: Forms
- Lesson: Form Attributes
- Lesson: Form Validation
- Lesson: Dialogs and Modals
- Lesson: Project: Accessible Contact Form