HTML forms are used to collect information from users.
After a user enters data, the information is usually sent to a server for processing.
Forms are commonly used for contact pages, login systems, registration forms, and surveys.
The <form> element is used to create a form in HTML.
The <Form> tag acts as a container for input elements such as:
The <input> element is the most frequently used form element.
It can display different types of input fields depending on the type attribute.
Common Input Types| Input Type | Function |
|---|---|
| text | Creates a single-line text field |
| radio | Lets the user select one option |
| submit | Sends form data to the server |
| button | Creates a clickable button |
A text input field allows users to enter a single line of text.
Important points:
The <label> element provides a description for an input field.
It is important because:
Screen readers read the label when the input is selected.
Clicking the label automatically selects the related input.
It improves accessibility and usability.
The for attribute of the <label> must match the id of the corresponding <input> element.
Radio buttons allow users to choose one option from multiple choices.
Note: All radio buttons in the same group must share the same name attribute.
Checkboxes allow users to select multiple options.
Users can select none, one, or multiple options.
The submit button sends form data to a server for processing.
The destination file is defined using the action attribute inside the <form> tag.
When the user clicks Submit, the form data is sent to process.php.
Every input field must have a name attribute.
If the name attribute is missing, the input value will not be included when the form is submitted.
Incorrect Example (Data will not be sent):<form> creates a form container.
<input> collects user data.
The type attribute defines the kind of input.
<label> improves accessibility and usability.
Radio buttons allow one selection.
Checkboxes allow multiple selections.
The submit button sends data to the server.
The name attribute is necessary for form submission.