HTML forms and input
HTML forms are used to collect user input.
An HTML form represents an area in a document that contains interactive controls and sends the collected user information to a Web server.
HTML forms typically contain various elements such as input fields, checkboxes, radio buttons, and drop-down lists.
The following is an example of a simple HTML form:
The element is used to create a form,actionthe attribute defines the target URL to which the form data is submitted,methodthe attribute defines the HTTP method used to submit the data ("post" is used here).The element is used to add labels to form elements, improving accessibility.The element is one of the most commonly used form elements; it can create text input fields, password fields, radio buttons, checkboxes, and more.typeThe attribute defines the type of the input field,idthe attribute is used to associateelements,namethe attribute is used to identify form fields.The element is used to create a drop-down list, whilethe element is used to define the options in the drop-down list.
Example
Try it »
Online example
Example
The following example creates a form with two input fields:
Try it »
Example
The following example creates a form with a regular input field and a password input field:
Try it »
(More examples can be found at the bottom of this page.)
HTML Forms
A form is an area that contains form elements.
Form elements allow users to input content in a form, such as: text fields (textarea), drop-down lists (select), radio buttons (radio-buttons), checkboxes (checkbox), and more.
We can use the
HTML Forms - Input Elements
The form tag most commonly used is the input tag 。
The input type is defined by the type attribute.
Next, we will introduce several commonly used input types.
Text Fields (Text Fields)
Text fields are defined through the tag; when a user needs to type letters, numbers, and other content into a form, text fields are used.
Example
The browser displays as follows:
** Note:** The form itself is not visible. Also, in most browsers, the default width of a text field is 20 characters.
Password Fields
Password fields are defined through the tag:
Example
The browser displays as follows:
**Note:** Password field characters are not displayed in plain text; instead, they are replaced with asterisks * or dots .
Radio Buttons
tag defines the radio button options in a form:
Example
The browser displays as follows:
Checkboxes (Checkboxes)
defines a checkbox.
Checkboxes allow one or more options to be selected:
Example
The browser displays as follows:
Submit Button (Submit)
defines a submit button.
When the user clicks the submit button, the form's content is sent to the server. The form's action attribute defines the server-side file name.
The action attribute performs relevant processing on the received user input data:
Example
The browser displays as follows:
If you type a few letters in the text box above and then click the submit button, the input data will be sent to html_form_action.php file, and that page will display the result of the input.
The example above has a method attribute, which defines how form data is submitted; it can have the following values:
-
post: Refers to the HTTP POST method, where form data is included in the form body and then sent to the server, used for submitting sensitive data such as usernames and passwords.
-
get: The default value, referring to the HTTP GET method, where form data is appended to the action attribute's URL, and with ?as a separator, generally used for non-sensitive information such as pagination. For example: https://www.runoob.com/?page=1, where page=1 is the data submitted by the get method.
Example
More examples
Radio Buttons (Radio buttons)
This example demonstrates how to create radio buttons in HTML.
Checkboxes (Checkboxes)
This example demonstrates how to create checkboxes in an HTML page. Users can select or deselect checkboxes.
A simple drop-down list
This example demonstrates how to create a simple drop-down list in an HTML page. A drop-down list is a selectable list.
A pre-selected drop-down list
This example demonstrates how to create a simple drop-down list with a pre-selected value.
Textarea (Textarea)
This example demonstrates how to create a textarea (a multi-line text input control). Users can write text in the textarea, with no limit on the number of characters.
Create a button
This example demonstrates how to create a button. You can customize the text on the button.
Form Examples
A form with a border
This example demonstrates how to draw a box with a caption around data.
A form with an input field and a submit button
This example demonstrates how to add a form to a page. This form contains two input fields and a submit button.
A form with checkboxes
This form contains two checkboxes and a submit button.
A form with radio buttons
This form contains two radio buttons and a submit button.
Sending email from a form
This example demonstrates how to send email from a form.
HTML Form Tags
New : HTML5 new tags