HTML forms are how websites collect information from you. When you sign up for a social media account like ModafVibe, log into your email, or search on Google, you are using an HTML form.
The form Element
The <form> element is a container for all the input elements like text fields, checkboxes, radio buttons, and submit buttons. Think of it like a digital paper form that users fill out.
<form>
<!-- form elements go here -->
</form>
By itself, the form tag does nothing visible. It just groups the input elements together.
The input Element
The <input> element is the most important form element. It can display as many different types of controls depending on the type attribute.
| Type | Description |
|---|---|
type="text"
| A single-line text input field |
type="radio"
| Radio button (select ONE option) |
type="checkbox"
| Checkbox (select ZERO or MORE options) |
type="submit"
| A button that sends the form data |
type="button"
| A clickable button (often with JavaScript) |
type="email"
| For email addresses (validates format) |
type="password"
| Hides characters as you type |
type="date"
| Shows a date picker |
type="email". For numbers, use type="number". This helps browsers validate input and show the right keyboard on mobile devices.
Text Fields (Single-line input)
Use <input type="text"> to create a single-line text field for names, addresses, or short answers.
The placeholder attribute shows hint text inside the field that disappears when the user starts typing.
action="/action_page.php" tells the browser where to send the form data. In a real website, this would be a server-side script that processes the information. For now, just understand the structure.
The label Element (Important for Accessibility)
The <label> tag is used to add a text label to a form field. It is important for screen readers and also makes the form easier to use.
When you click on a label, it automatically focuses the input field. This is especially helpful for small checkboxes and radio buttons.
To connect a label to an input, the for attribute of the label must match the id attribute of the input.
Radio Buttons (Select ONE)
Radio buttons allow the user to select exactly ONE option from a group. All radio buttons in a group must share the same name attribute.
Notice you can only select one option. That is how radio buttons work. If you try to select another, the first one unselects automatically.
Checkboxes (Select ZERO or MORE)
Checkboxes allow users to select zero, one, or multiple options from a list. Unlike radio buttons, checkboxes do not need to share the same name.
You can select as many as you want. You can also select none at all. That is the difference between radio buttons (select one) and checkboxes (select many).
The Submit Button (Send the Form)
The submit button sends the form data to a server for processing. The action attribute tells the browser where to send the data.
The method="post" tells the browser to send the data securely (as opposed to method="get" which puts data in the URL).
/action_page.php. On a real website, that file would process the data (save to a database, send an email, etc.). Since we are just learning HTML, the form will not actually submit anywhere. Later you will learn backend languages like PHP, Python, or Node.js to handle form data.
The Name Attribute (Required!)
Every input field that needs to be submitted must have a name attribute. Without a name, the input value will not be sent to the server.
Think of the name as the key in a key-value pair. The name is “username”, the value is what the user types.
Correct (Has name attribute)
<input type="text" id="username" name="username">
This value will be sent to the server.
Wrong (Missing name attribute)
<input type="text" id="username">
This value will NOT be sent to the server.
id attribute is for connecting labels and CSS/JavaScript. The name attribute is for sending data to the server. You usually need both.
Complete Form Example
Here is a complete form combining all the elements we learned:
Quick Summary
- Use
<form>to wrap all your form elements. - Use
action="/action_page.php"to specify where to send form data (in real websites, this points to a server-side script). - Use
<label>withforattribute to connect to inputs. This improves accessibility. - Use
type="text"for single-line text inputs. - Use
type="radio"when users can select only ONE option. - Use
type="checkbox"when users can select ZERO or MORE options. - Use
type="submit"to send the form data. - Always give each input a
nameattribute. Without it, the value will not be sent. - Use
placeholderfor hint text inside the field. - Use
requiredto make a field mandatory.
What Just Happened? Let Me Explain
- Forms are how the web collects data. Every time you sign up for a website like ModafVibe, you are filling out an HTML form.
- The
actionattribute tells the browser where to send the data. In real websites, this is a backend script that processes the information. - The
nameattribute is critical. It tells the server what each piece of data is called. Think of it like a question: “What is your fullname?” The answer is whatever the user types. - Radio buttons are named the same so only one can be selected. If you give them different names, they become independent and the user could select multiple.
- Checkboxes are independent by default. That is why they usually have different names.
HTML Forms Reference
Here are the tags and attributes we covered in this chapter:
| Tag / Attribute | Description |
|---|---|
<form>
| Container for form elements |
<input>
| Creates different form controls |
<label>
| Adds a text label to a form field |
type
| Specifies what kind of input (text, radio, checkbox, submit) |
name
| Required for submitting data to the server |
id
| Connects label to input (must match for attribute) |
placeholder
| Shows hint text inside the input |
required
| Makes the field mandatory to fill |