HTML Forms
Forms are how a web page collects information from people: sign-up pages, search boxes, checkout pages, surveys and comment boxes are all built with HTML forms. The <form> element groups input controls together and defines what happens when the user submits them.
The form element
<form action="/signup" method="post">
<!-- form controls go here -->
</form>The <form> element itself is invisible. Inside it you place controls such as text boxes, checkboxes, radio buttons and a submit button.
A first form
<form action="#">
<label for="first">First name:</label><br>
<input type="text" id="first" name="firstName" value="Linh"><br>
<label for="last">Last name:</label><br>
<input type="text" id="last" name="lastName" value="Tran"><br><br>
<input type="submit" value="Submit">
</form>Here action="#" just means "stay on this page", because these playgrounds have no server to receive the data. On a real site, action is the URL of the program that processes the form.
The input element
<input> is the most important form control. Its type attribute completely changes how it looks and behaves:
| Type | Displays |
|---|---|
text |
A single-line text field |
password |
A text field that hides what you type |
radio |
A round button; only one per group can be chosen |
checkbox |
A square box; any number can be ticked |
submit |
A button that sends the form |
button |
A clickable button with no default action |
You will see many more types (email, date, color, range…) in the Input Types lesson.
Labels
The <label> element gives a control a visible, readable name. Connect the two by giving the label a for attribute equal to the input's id. This brings two benefits:
- Screen readers announce the label when the input gets focus.
- Clicking the label text focuses the field or toggles the checkbox — a bigger target, which helps on phones.
You can also wrap the control inside the label, and then for is not needed: <label><input type="checkbox"> Remember me</label>.
Radio buttons and checkboxes
Radio buttons that share the same name form one group, so picking one deselects the others. Checkboxes are independent.
<form action="#">
<p>Choose a drink:</p>
<label><input type="radio" name="drink" value="coffee" checked> Coffee</label><br>
<label><input type="radio" name="drink" value="tea"> Tea</label><br>
<label><input type="radio" name="drink" value="juice"> Juice</label>
<p>Extras:</p>
<label><input type="checkbox" name="extra" value="sugar"> Sugar</label><br>
<label><input type="checkbox" name="extra" value="milk"> Milk</label><br>
<label><input type="checkbox" name="extra" value="ice"> Ice</label>
</form>The name attribute matters
Only controls with a name attribute are sent when the form is submitted. Each one becomes a name=value pair. An input without name is silently left out — a very common beginner bug.
The following playground intercepts the submit event with JavaScript and displays the pairs the browser would send, instead of loading a new page.
<style>
form { display: grid; gap: 8px; max-width: 260px; font-family: sans-serif; }
pre { background: #f4f4f4; padding: 8px; }
</style>
<form id="order">
<label>Name <input name="customer" value="Minh"></label>
<label>Quantity <input type="number" name="qty" value="2"></label>
<label>Note (no name!) <input value="I will not be sent"></label>
<label><input type="checkbox" name="gift" value="yes" checked> Gift wrap</label>
<button type="submit">Place order</button>
</form>
<pre id="result">Press the button…</pre>
<script>
document.getElementById('order').addEventListener('submit', (event) => {
event.preventDefault(); // don't leave the page
const data = new FormData(event.target);
const lines = [];
for (const [key, value] of data) lines.push(key + ' = ' + value);
document.getElementById('result').textContent = lines.join('\n');
});
</script>Notice that the "Note" field never shows up in the result because it has no name.
How the data travels
| Step | What happens |
|---|---|
| 1 | The user fills in the controls and clicks submit (or presses Enter) |
| 2 | The browser checks built-in validation rules |
| 3 | It collects every named, enabled control into name=value pairs |
| 4 | It sends them to the action URL using the method (GET or POST) |
| 5 | The server responds with a new page, which the browser shows |
Tip: Always pair every input with a
<label>. Placeholder text alone is not a label — it disappears as soon as the user starts typing.
Note: HTML only builds and sends the form. Saving data, sending emails or logging users in is done by a server program written in a language like PHP, Python, Node.js or Java.
Test yourself
Which attribute must an input have for its value to be submitted?
idnamevaluelabel
How do you make several radio buttons act as one group?
- Give them the same
id - Put them in a
<group>tag - Give them the same
name - Give them the same
value
How is a <label> connected to an input using attributes?
label'sforequals the input'sidlabel'snameequals the input'snamelabel'sidequals the input'sfor- They connect automatically by order