To gather user input, an HTML forms is utilized. Most of the time, a server receives user input and processes it.
Example
The < form > Element
To build an HTML form for user input, use the < form > element in HTML:
A variety of input components, including text fields, checkboxes, radio buttons, submit buttons, and more, can be contained within the <form> element.
The < input > Element
The HTML <input> element is the most used form element.
An <input> element can be displayed in many ways, depending on the type attribute.
Here are some examples:
Type
Description
<input type="text">
Displays a single-line text input field
<input type="radio">
Displays a radio button (for selecting one of many choices)
<input type="checkbox">
Displays a checkbox (for selecting zero or more of many choices)
<input type="submit">
Displays a submit button (for submitting the form)
<input type="button">
Displays a clickable button
Text Fields
The <input type=”text”> 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:
The < label > Element
Take note of how the <label> element is used in the previous example.
For many form elements, a label is defined using the <label> tag.
Users of screen readers will find the <label> element helpful since it will read the label aloud when the user concentrates on the input element.
Because it toggles the radio button or checkbox when the user clicks within it, the <label> element also aids users who have trouble clicking on small regions, like radio buttons or checkboxes.
To link them together, the idattribute of the <input> element and the forattribute of the <label> tag must be equivalent.
Radio Buttons
The <input type=”radio”> 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:
The HTML code above will appear in a browser like this:
Select your preferred web language:
Checkboxes
The <input type=”checkbox”> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
A form with checkboxes:
This is how the HTML code above will be displayed in a browser:
The Submit Button
The button to submit form data to a form-handler is defined by the <input type=”submit”>.
Usually, the form-handler is a server file that contains a script to process incoming data.
The actionattribute of the form specifies the form-handler.
Example
A form with a submit button:
This is how the HTML code above will be displayed in a browser:
The Name Attribute for < input >
Observe that in order for an input field to be submitted, a nameattribute is required.
The input field’s value won’t be delivered at all if the nameattribute is left off.
Example
This example will not submit the value of the “First name” input field: