- HTML Tag
- Definition and Usage
- Browser Support
- Attributes
- HTML Forms
- The HTML element
- Text input
- Example of the text input:
- Radio Button Input
- Example of the radio button input:
- Submit input
- Example of the submit input:
- The Action Attribute
- The Target Attribute
- The Method Attribute
- Example of the GET method:
- Example of the POST method:
- When to use the GET Method
- When to use the POST Method
- Other Attributes
- HTML Forms
- Example
- The Element
- The Element
- Text Fields
- Example
- The Element
- Radio Buttons
- Example
- Checkboxes
- Example
- The Submit Button
- Example
- Example
HTML Tag
An HTML form with two input fields and one submit button:
More «Try it Yourself» examples below.
Definition and Usage
The tag is used to create an HTML form for user input.
The element can contain one or more of the following form elements:
Browser Support
Attributes
Attribute | Value | Description |
---|---|---|
accept-charset | character_set | Specifies the character encodings that are to be used for the form submission |
action | URL | Specifies where to send the form-data when a form is submitted |
autocomplete | on off | Specifies whether a form should have autocomplete on or off |
enctype | application/x-www-form-urlencoded multipart/form-data text/plain | Specifies how the form-data should be encoded when submitting it to the server (only for method=»post») |
method | get post | Specifies the HTTP method to use when sending form-data |
name | text | Specifies the name of a form |
novalidate | novalidate | Specifies that the form should not be validated when submitted |
rel | external help license next nofollow noopener noreferrer opener prev search | Specifies the relationship between a linked resource and the current document |
target | _blank _self _parent _top | Specifies where to display the response that is received after submitting the form |
HTML Forms
An HTML form is composed of form elements, which are different kinds of input elements, such as checkboxes, text fields, submit buttons, radio buttons, and so on.
The HTML element
Let’s speak about some of input types.
Text input
Example of the text input:
html> html> head> title>Title of the document title> head> body> h2>Text Input Example h2> form> Name:br> input type="text" name="name"> br> Surname:br> input type="text" name="surname"> form> body> html>
Radio Button Input
Example of the radio button input:
html> html> head> title>Title of the document title> head> body> h2>Radio Button Example h2> form> input type="radio" name="game"value="football" checked> Football input type="radio" name="game" value="basketball"> Basketball form> body> html>
Submit input
The submits the form data to a form-handler.
The form-handler is a server page with a script to process input data, which is defined in the action attribute of the form.
Example of the submit input:
html> html> head> title>Title of the document title> head> body> h2>HTML Form Example h2> form action="/form/submit" method="POST"> Name:br> input type="text" name="firstname" value="Tom"> br> Surname:br> input type="text" name="lastname" value="Brown"> br> Age:br> input type="text" name="Age" value="21"> br>br> input type="submit" value="Submit"> form> p>Click the "Submit" button, to sent the form-data to the action page. p> body> html>
The Action Attribute
The action attribute specifies the action that should be performed when the form is submitted.
When the user the form data is sent to a web page on the server when the user clicks on the submit button.
The Target Attribute
The target attribute defines whether the form result is open in a new browser tab, frame, or in the current window.
The default value of this attribute is _self . Using this value will open the form result in the current window.
The _blank value will open the form result open in a new browser tab.
form action="/form/submit" target="_blank">
The Method Attribute
The method attribute defines the HTTP method (GET or POST) that will be used when submitting the form data.
Example of the GET method:
html> html> head> title>Title of the document title> head> body> h2>The method Attribute With the GET Method h2> form action="/form/submit" target="_blank" method="GET"> Neame:br> input type="text" name="name" value="Tom"> br> Surname:br> input type="text" name="Surname" value="Brown"> br> Age:br> input type="number" name="Aage" value="21"> br>br> input type="submit" value="Submit"> form> p> Here we used the "_blank" value, which will open the form result in a new browser tab. p> body> html>
Example of the POST method:
html> html> head> title>Title of the document title> head> body> h2>The method Attribute With the Post Method h2> form action="/form/submit" target="_blank" method="POST"> Name:br> input type="text" name="name" value="Tom"> br> Surname:br> input type="text" name="surname" value="Brown"> br> Age:br> input type="number" name="age" value="21"> br>br> input type="submit" value="Submit"> form> body> html>
When to use the GET Method
GET is the default method when submitting form data, and when using this method the form data is visible in the page address field.
When to use the POST Method
If the form data includes sensitive or personal information, always use the POST method, as it doesn’t display the submitted form data in the page address field.
As there are no size limitations while using the POST method, it can be used to send large amounts of data.
Form submissions with the POST method can’t be bookmarked.
Other Attributes
Below you can find other attributes:
Attribute | Description |
---|---|
accept-charset | This attribute defines the charset that is used in the submitted form (default: the page charset). |
autocomplete | This attribute defines whether the browser should autocomplete the form or not (default: on). |
enctype | This attribute defines the encoding of the submitted data (default:url-encoded). |
name | This attribute defines a name that is used to identify the form. |
novalidate | This attribute defines that the browser must not validate the form. |
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server for processing.
Example
The Element
The HTML element is used to create an HTML form for user input:
The element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.
All the different form elements are covered in this chapter: HTML Form Elements.
The Element
The HTML element is the most used form element.
An element can be displayed in many ways, depending on the type attribute.
Type | Description |
---|---|
Displays a single-line text input field | |
Displays a radio button (for selecting one of many choices) | |
Displays a checkbox (for selecting zero or more of many choices) | |
Displays a submit button (for submitting the form) | |
Displays a clickable button |
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The defines a single-line input field for text input.
Example
A form with input fields for text:
This is how the HTML code above will be displayed in a browser:
Note: The form itself is not visible. Also note that the default width of an input field is 20 characters.
The Element
Notice the use of the element in the example above.
The tag defines a label for many form elements.
The element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focuses on the input element.
The element also helps users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) — because when the user clicks the text within the element, it toggles the radio button/checkbox.
The for attribute of the tag should be equal to the id attribute of the element to bind them together.
Radio Buttons
The defines a radio button.
Radio buttons let a user select ONE of a limited number of choices.
Example
A form with radio buttons:
Choose your favorite Web language:
This is how the HTML code above will be displayed in a browser:
Choose your favorite Web language:
Checkboxes
The defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
I have a boat
The Submit Button
The defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input data.
The form-handler is specified in the form’s action attribute.
Example
A form with a submit button:
This is how the HTML code above will be displayed in a browser:
The Name Attribute for
Notice that each input field must have a name attribute to be submitted.
If the name attribute is omitted, the value of the input field will not be sent at all.
Example
This example will not submit the value of the «First name» input field: