Lesson 10 of 296
Forms and inputs
What you will learn
- How a form sends data to a server
- The
<input>element and its many types - Labels and why every field needs one
- Text areas, drop-down lists, checkboxes and radio buttons
- Buttons and the most common form bug
On this page
What a form is
A form collects information from the user and sends it to a server. Login pages, sign-up pages, search boxes, checkout pages and contact pages are all forms.
<form action="/subscribe" method="POST">
<label for="email">Email address</label>
<input type="email" id="email" name="email">
<button type="submit">Subscribe</button>
</form>| Part | Meaning |
|---|---|
action | The URL that receives the data |
method | How the data is sent: GET or POST |
name | The key for this field's value in the data sent to the server |
When the user clicks Subscribe, the browser sends email=sam@example.com to /subscribe.
A field without a name is not sent. This is a very common bug.
GET or POST?
| Method | How data is sent | Use for |
|---|---|---|
GET | Added to the URL: /search?q=shoes | Searching and filtering. Safe to bookmark and share. |
POST | Inside the body of the request, not visible in the URL | Anything that creates or changes data: sign-up, login, payment, orders |
Never send passwords with GET. They would appear in the address bar, the browser history and server logs.
Labels
Every field needs a label that says what to type. Connect the label to the field: the label's for must match the input's id.
<label for="first-name">First name</label>
<input type="text" id="first-name" name="firstName">Why this matters:
- Screen readers read the label when the user reaches the field.
- Clicking the label puts the cursor in the field. This gives a bigger area to tap on phones.
A placeholder is not a label. Placeholder text disappears when the user starts typing, so they forget what the field was for. It is also often too pale to read.
<input type="text" placeholder="e.g. Sam"> only a hint, not a labelInput types
The type attribute changes how a field works. On phones, the right type also shows the right keyboard.
| Type | Use for | Extra behaviour |
|---|---|---|
text | Short text | - |
email | Email addresses | Checks for an @ sign; shows @ on phone keyboards |
password | Passwords | Hides the characters |
number | Amounts | Only numbers; up and down arrows |
tel | Phone numbers | Shows a number pad on phones |
url | Web addresses | Checks the format |
date | Dates | Shows a date picker |
time | Times | Shows a time picker |
search | Search boxes | May show a clear (x) button |
checkbox | Yes/no, or choose many | - |
radio | Choose exactly one from a group | - |
file | Uploading files | Opens the file picker |
hidden | Data the user does not see | Not shown at all |
range | A slider | - |
Tip: use type="tel" or type="text" for phone numbers and card numbers, not type="number". They are not amounts: a phone number can start with 0 and should not have up and down arrows.
Textarea: long text
<label for="message">Message</label>
<textarea id="message" name="message" rows="5"></textarea>Select: a drop-down list
<label for="country">Country</label>
<select id="country" name="country">
<option value="">Please choose</option>
<option value="uk">United Kingdom</option>
<option value="ie">Ireland</option>
</select>The value is what is sent to the server. The text between the tags is what the user sees.
Checkboxes: choose any number
<input type="checkbox" id="news" name="newsletter" value="yes">
<label for="news">Send me the newsletter</label>If the box is not ticked, nothing is sent for that field.
Radio buttons: choose exactly one
All radio buttons in one group must share the same name. That is what links them.
<fieldset>
<legend>Delivery</legend>
<input type="radio" id="std" name="delivery" value="standard" checked>
<label for="std">Standard</label>
<input type="radio" id="exp" name="delivery" value="express">
<label for="exp">Express</label>
</fieldset><fieldset> groups related fields. <legend> is the title of the group. Screen readers say "Delivery, Standard" so the user knows what the choice is about. checked selects an option by default.
Buttons and the common bug
<button type="submit">Send</button> sends the form
<button type="button">Show more</button> does nothing by itself; used with JavaScript
<button type="reset">Clear</button> empties every field (rarely a good idea)The bug: a <button> inside a form with no type acts as submit. So a "Show password" button with no type will send the form by accident. Always write the type.
Other useful attributes
| Attribute | What it does |
|---|---|
value | The starting value |
placeholder | A hint shown when the field is empty |
disabled | The field cannot be used and is not sent |
readonly | The field cannot be changed but is sent |
autocomplete | Helps the browser fill the field, for example autocomplete="email" or "current-password" |
autofocus | Puts the cursor in this field when the page loads |
A complete example
<form action="/register" method="POST">
<label for="name">Full name</label>
<input type="text" id="name" name="name" autocomplete="name">
<label for="reg-email">Email</label>
<input type="email" id="reg-email" name="email" autocomplete="email">
<label for="pw">Password</label>
<input type="password" id="pw" name="password" autocomplete="new-password">
<input type="checkbox" id="terms" name="terms" value="accepted">
<label for="terms">I accept the terms</label>
<button type="submit">Create account</button>
</form>Common mistakes
- Missing
name, so the value is never sent. - Using placeholder instead of a label.
- A button with no
typeinside a form. - Radio buttons with different names, so the user can tick all of them.
type="number"for phone numbers.- Sending passwords with GET.
How to say it in an interview
"A form sends each field's name and value to the URL in action, using GET for searches and POST for anything that changes data. Every field has a label connected with for and id, because a placeholder is not a label. I choose the right input type so phones show the right keyboard. Radio buttons in a group share a name. And I always set the type on buttons, because a button in a form defaults to submit."
Key words
| Word | Meaning |
|---|---|
| Form | A group of fields that sends data to a server |
| action | Where the form data is sent |
| method | How it is sent: GET or POST |
| name | The key for a field's value |
| Label | The visible title of a field |
| Placeholder | A light hint inside an empty field |
| Fieldset / legend | A group of related fields and its title |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.