- : The Form element
- Try it
- Attributes
- Attributes for form submission
- Examples
- Result
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Sending parameters to another web page
- 1) Understanding the format of URL’s parameters
- 2) Values are sent from the form, to the server
- Sending data without form
- 3) Extracting data received from the URL in the page
- 4) Updating the page with data received
- 5) Now, testing the script
: The Form element
The HTML element represents a document section containing interactive controls for submitting information.
Try it
It is possible to use the :valid and :invalid CSS pseudo-classes to style a element based on whether the elements inside the form are valid.
Attributes
This element includes the global attributes.
Comma-separated content types the server accepts.
Note: This attribute has been deprecated and should not be used. Instead, use the accept attribute on elements.
Space-separated character encodings the server accepts. The browser uses them in the order in which they are listed. The default value means the same encoding as the page. (In previous versions of HTML, character encodings could also be delimited by commas.)
A nonstandard attribute used by iOS Safari that controls how textual form elements should be automatically capitalized. autocapitalize attributes on a form elements override it on . Possible values:
- none : No automatic capitalization.
- sentences (default): Capitalize the first letter of each sentence.
- words : Capitalize the first letter of each word.
- characters : Capitalize all characters — that is, uppercase.
Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on . Possible values:
- off : The browser may not automatically complete entries. (Browsers tend to ignore this for suspected login forms; see The autocomplete attribute and login fields.)
- on : The browser may automatically complete entries.
The name of the form. The value must not be the empty string, and must be unique among the form elements in the forms collection that it is in, if any.
Controls the annotations and what kinds of links the form creates. Annotations include external , nofollow , opener , noopener , and noreferrer . Link types include help , prev , next , search , and license . The rel value is a space-separated list of these enumerated values.
Attributes for form submission
The following attributes control behavior during form submission.
The URL that processes the form submission. This value can be overridden by a formaction attribute on a , , or element. This attribute is ignored when method=»dialog» is set.
If the value of the method attribute is post , enctype is the MIME type of the form submission. Possible values:
- application/x-www-form-urlencoded : The default value.
- multipart/form-data : Use this if the form contains elements with type=file .
- text/plain : Useful for debugging purposes.
This value can be overridden by formenctype attributes on , , or elements.
The HTTP method to submit the form with. The only allowed methods/values are (case insensitive):
- post : The POST method; form data sent as the request body.
- get (default): The GET ; form data appended to the action URL with a ? separator. Use this method when the form has no side effects.
- dialog : When the form is inside a , closes the dialog and causes a submit event to be fired on submission, without submitting data or clearing the form.
This value is overridden by formmethod attributes on , , or elements.
This Boolean attribute indicates that the form shouldn’t be validated when submitted. If this attribute is not set (and therefore the form is validated), it can be overridden by a formnovalidate attribute on a , , or element belonging to the form.
Indicates where to display the response after submitting the form. It is a name/keyword for a browsing context (for example, tab, window, or iframe). The following keywords have special meanings:
- _self (default): Load into the same browsing context as the current one.
- _blank : Load into a new unnamed browsing context. This provides the same behavior as setting rel=»noopener» which does not set window.opener .
- _parent : Load into the parent browsing context of the current one. If no parent, behaves the same as _self .
- _top : Load into the top-level browsing context (i.e., the browsing context that is an ancestor of the current one and has no parent). If no parent, behaves the same as _self .
This value can be overridden by a formtarget attribute on a , , or element.
Examples
form method="get"> label> Name: input name="submitted-name" autocomplete="name" /> label> button>Savebutton> form> form method="post"> label> Name: input name="submitted-name" autocomplete="name" /> label> button>Savebutton> form> form method="post"> fieldset> legend>Do you agree to the terms?legend> label>input type="radio" name="radio" value="yes" /> Yeslabel> label>input type="radio" name="radio" value="no" /> Nolabel> fieldset> form>
Result
Technical summary
Content categories | Flow content, palpable content |
---|---|
Permitted content | Flow content, but not containing elements |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts flow content |
Implicit ARIA role | form if the form has an accessible name, otherwise no corresponding role |
Permitted ARIA roles | search , none or presentation |
DOM interface | HTMLFormElement |
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 13, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Sending parameters to another web page
For this purpose a form is created whose values will be transmitted automatically, and in the target page, a script retrieves the values sent.
We have seen how to create a form, we will detail here how to extract the transmitted data.
1) Understanding the format of URL’s parameters
Three symbols are used to define a string of parameters to pass:
? concatenates the URL and the string of parameters. & separates multiple parameters. = assigns a value to the variable.
https://www.xul.fr/demo.html?login="me"&password="1234"
In this example, we have two parameters, login and password, which are assigned the values «me» and «1234».
2) Values are sent from the form, to the server
You have nothing to do to send the values: all variables and values in a form are sent automatically providing the action of the form is a page to load.
The attribute «name» of each form item will provide the name of the variable and the attribute «value» its value.
See at the source of the form at bottom.
The GET method appends the data to the URL, while the POST method would transmit them directly.
Sending data without form
To pass parameters to another page or a script without displaying a form (but with a form tag), we use the «hidden» field:
This invisible form will pass to otherpage.html the parameter: varname=12345.
3) Extracting data received from the URL in the page
The location.search attribute contains the chain of parameters, it remains to be analyzed.
Here is the complete code to process data sent:
- location.search is the property that holds the list of parameters.
- substring(1) skips the ? symbol and returns the string minus this sign.
- split(«&») splits the string and returns an array whose elements are the parameters.
- this array is assigned to the «parameters» variable. We can now access individual elements by subscripting the array. Parameters[0] is the first element.
- we have to split again the parameter into another small array that holds the name of the variable and the value.
- in this example, we need only for the value, so we subscript the small array to second item, temp[1].
- the unescape function convert special characters.
- we have assigned the l variable with the login value and the p variable with the password.
- the login is written in the log field thanks to the getElementById method.
- and password to the pass field.
4) Updating the page with data received
In this example, I suppose we want to write the data into the page that is loaded with the parameters.
The login variable has been assigned in the previous code.
Two fields have been defined in the page:
The fields are identified by the id property. To fill them with data we have to use the DOM’s method getElementById(«») and the innerHTML property.
getElementById("log").innerHTML = login;
5) Now, testing the script
Fill the fields below and click on the button.