Learn how to build forms (input forms) that collect data from users.
1. What Is a Form?
An HTML form is the interface through which users enter data and send it to a server.
It is used in almost every web service, including sign-ups, logins, search, and surveys.
2. The form Tag
<form action="URL to handle the data" method="get or post">
<!-- input elements -->
</form>
Main Attributes
| Attribute | Description | Value |
|---|---|---|
action |
Server URL the form data is sent to | A URL |
method |
How the data is sent | get or post |
<!-- GET: data is exposed in the URL. Used for search, etc. -->
<form action="/search" method="get">
<input type="text" name="query">
<button type="submit">Search</button>
</form>
<!-- URL after submitting: /search?query=entered-value -->
<!-- POST: data is not exposed in the URL. Used for sign-ups, logins, etc. -->
<form action="/login" method="post">
<input type="email" name="email">
<input type="password" name="password">
<button type="submit">Log in</button>
</form>
3. The input Tag — Various Types
<input> is a void element, so it has no closing tag.
text — Text Input
<input type="text" name="username" placeholder="Enter your name" maxlength="20">
email — Email Input
<input type="email" name="email" placeholder="[email protected]">
<!-- An error is shown automatically if the value is not an email format (contains @) -->
password — Password Input
<input type="password" name="password" minlength="8">
<!-- What you type is masked with ••• -->
number — Number Input
<input type="number" name="age" min="0" max="150" step="1" value="20">
<!-- min: minimum value, max: maximum value, step: increment size -->
checkbox — Checkboxes
<!-- Multiple selections allowed -->
<input type="checkbox" name="hobby" value="reading" id="hobby-reading">
<label for="hobby-reading">Reading</label>
<input type="checkbox" name="hobby" value="coding" id="hobby-coding" checked>
<label for="hobby-coding">Coding</label>
<!-- checked: checked by default -->
radio — Radio Buttons
<!-- Only one can be selected within the same name group -->
<input type="radio" name="gender" value="male" id="male">
<label for="male">Male</label>
<input type="radio" name="gender" value="female" id="female">
<label for="female">Female</label>
<input type="radio" name="gender" value="prefer-not-to-say" id="prefer">
<label for="prefer">Prefer not to say</label>
file — File Upload
<!-- Allow image files only -->
<input type="file" name="profile-img" accept="image/*">
<!-- Allow selecting multiple files -->
<input type="file" name="documents" accept=".pdf,.doc,.docx" multiple>
date — Date Picker
<input type="date" name="birthday" min="1900-01-01" max="2026-12-31">
range — Slider
<input type="range" name="satisfaction" min="1" max="10" value="5">
hidden — Hidden Value
<!-- Data the user cannot see but that is sent along with the form -->
<input type="hidden" name="user_id" value="12345">
Other input Types
<input type="tel" placeholder="555-000-0000"> <!-- Phone number -->
<input type="url" placeholder="https://example.com"> <!-- URL -->
<input type="color"> <!-- Color picker -->
<input type="search" placeholder="Enter a search term"> <!-- Search -->
4. The label Tag
<label> connects an input element with its descriptive text.
<!-- Method 1: connect with the for attribute and id (recommended) -->
<label for="username">Name:</label>
<input type="text" id="username" name="username">
<!-- Method 2: the label wraps the input -->
<label>
Name:
<input type="text" name="username">
</label>
Why labels matter: Clicking a label focuses the connected input element.
Screen readers read labels too, so they are essential for accessibility.
5. select — Dropdown Selection
<label for="city">City of residence:</label>
<select name="city" id="city">
<option value="">-- Select a city --</option> <!-- Indicates that nothing is selected by default -->
<option value="new-york" selected>New York</option> <!-- selected: the default selection -->
<option value="los-angeles">Los Angeles</option>
<option value="chicago">Chicago</option>
<option value="houston">Houston</option>
</select>
<!-- Grouping with optgroup -->
<select name="job">
<optgroup label="IT">
<option value="dev">Developer</option>
<option value="design">Designer</option>
</optgroup>
<optgroup label="General">
<option value="sales">Sales</option>
<option value="marketing">Marketing</option>
</optgroup>
</select>
<!-- multiple: allows several selections (together with the Ctrl/Cmd key) -->
<select name="skills" multiple size="4">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
<option value="python">Python</option>
</select>
6. textarea — Multi-line Text Input
<label for="message">Message:</label>
<textarea
id="message"
name="message"
rows="5"
cols="40"
placeholder="Type your message here..."
maxlength="500"
></textarea>
<!-- rows: number of rows to display, cols: number of columns to display -->
7. The button Tag
<!-- type="submit": submits the form (default) -->
<button type="submit">Submit</button>
<!-- type="reset": resets the form -->
<button type="reset">Start over</button>
<!-- type="button": a plain button (used together with JavaScript) -->
<button type="button" onclick="alert('Clicked!')">Click</button>
8. fieldset and legend — Grouping a Form
Groups related input elements together.
<form>
<fieldset>
<legend>Personal Information</legend> <!-- the title of the fieldset -->
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</fieldset>
<fieldset>
<legend>Account Information</legend>
<label for="pw">Password:</label>
<input type="password" id="pw" name="password"><br>
<label for="pw2">Confirm password:</label>
<input type="password" id="pw2" name="password_confirm">
</fieldset>
<button type="submit">Sign up</button>
</form>
9. Validation Attributes
<!-- required: a required field (the form cannot be submitted while it is empty) -->
<input type="text" name="name" required>
<!-- placeholder: hint text shown in gray before the user types -->
<input type="text" name="name" placeholder="John Doe">
<!-- disabled: disabled (cannot be edited or submitted) -->
<input type="text" name="name" disabled>
<!-- readonly: read-only (cannot be edited, but is still submitted) -->
<input type="text" name="name" value="John Doe" readonly>
<!-- minlength, maxlength: minimum/maximum text length -->
<input type="text" name="username" minlength="2" maxlength="20">
<!-- min, max: minimum/maximum numeric value -->
<input type="number" name="age" min="1" max="150">
<!-- pattern: validates the input format with a regular expression -->
<input type="text" name="phone" pattern="[0-9]{3}-[0-9]{4}-[0-9]{4}" placeholder="555-1234-5678">
Summary
| Tag/Attribute | Description |
|---|---|
<form> |
Form container |
action |
URL the data is sent to |
method |
How the data is sent (get/post) |
<input type="text"> |
Text input |
<input type="email"> |
Email input (format validation) |
<input type="password"> |
Password (masked) |
<input type="number"> |
Number input |
<input type="checkbox"> |
Multiple selection |
<input type="radio"> |
Single selection |
<input type="file"> |
File upload |
<input type="date"> |
Date picker |
<select> |
Dropdown selection |
<textarea> |
Multi-line text |
<button> |
Button |
<label> |
Input label |
<fieldset> |
Form group |
<legend> |
fieldset title |
required |
Required input |
placeholder |
Hint text |
disabled |
Disabled |
Next Steps
- View the example file - Sign-up form example
- Check the assignment - Build a survey page
- Next lesson: Semantic HTML