8 / 10 · Intro
Forms basics
Click the word Email next to a well-built form field and the cursor jumps into the box. That small detail is the label element doing its job.
Jump to the 10 practice questionsYou will learn
- Connect every control to a label
- Pick input types and group choices with fieldset and legend
- Use submit buttons and the browser's built-in checks
Before this: Tables
1Explain
What a form does
Search boxes, sign-up pages and checkout screens are all forms. A <form> element groups the controls, and when the visitor submits it, the browser sends each control's name together with its value to the address in the form's action attribute. The method attribute says how. get puts the data in the address, which suits searches you might bookmark.
The preview is sandboxed, so pressing a button sends nothing. Click a label and watch its field get the cursor.
<form action="/subscribe" method="post">
<label for="name">Name</label>
<input id="name" name="name" type="text" autocomplete="name">
<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email">
<button type="submit">Subscribe</button>
</form>2Explore
Input types
The type attribute of <input> changes the control. text is the default single-line box. email, tel and url look similar but show a matching keyboard on phones, and the browser checks that an email address has the right shape. number accepts digits and can have min and max.
The same input element becomes a different control for each type.
Tap a choice: the code and the preview change together.
<label for="f">Your answer</label>
<input id="f" type="text">Nothing is drawn: there is no visible content.
3Problem
for="phone"
A label has for="phone". Which input does it belong to?
Hint
for matches an id.
Solution
Answer: The input with id="phone"
The label’s for value matches the input’s id. That match connects the label to its input.
4Problem
An email field
Hint
The type has the same name as what it holds.
Solution
Answer: email
type="email".
5Explain
Example: connecting a label
A postcode field, built one decision at a time.
- Give the input an id, here pc. It must be unique on the page.
- Write the label with the same value in for: for="pc". Clicking the word Postcode now puts the cursor in the field.
- Give it a name: postcode. That is the name the browser sends together with the value.
- Add required, so the form cannot be sent while the field is empty, and autocomplete so the browser can offer a saved postcode.
<label for="pc">Postcode</label>
<input id="pc" name="postcode" type="text" autocomplete="postal-code" required>6Problem
The name that is sent
A field has id="pc" and name="postcode". Under which name does the browser send its value?
Hint
id connects the label; another attribute is for the server.
Solution
Answer: postcode
The form sends name=value pairs, so the server receives postcode=… . id only links the label to the field.
7Explain
Grouping choices
Radio buttons that belong to one question share the same name, and only one of them can be chosen at a time. Wrap the group in <fieldset> and put the question in <legend>. A screen reader then reads the question along with each option: Loaf size, Small, radio button, 1 of 2. Each radio button still gets its own label.
<fieldset>
<legend>Loaf size</legend>
<label><input type="radio" name="size" value="small"> Small</label>
<label><input type="radio" name="size" value="large"> Large</label>
</fieldset>8Problem
Radio buttons that exclude each other
Two radio buttons must exclude each other. What must they share?
Hint
The browser groups radio buttons by one attribute.
Solution
Answer: name
Radios with the same name form one group.
9Problem
Any number of choices
Visitors may pick any number of toppings, including none. Which input type fits?
Hint
Radio buttons in one group allow only one choice.
Solution
Answer: checkbox
Each checkbox is on or off by itself. radio allows one choice per group, and range picks one value on a slider.
10Problem
A search you can bookmark
A search form should give results that can be bookmarked. Which method fits?
Hint
One method puts the data into the address.
Solution
Answer: get
get puts the search words into the address, so the results page has an address you can save. post sends the data in the body of the request instead.
11Explain
Buttons and built-in checks
A <button> inside a form submits it by default; writing type="submit" makes that clear, and type="button" makes a button that does nothing until JavaScript gives it a job. Before sending, the browser can check fields for you. required stops the form if the field is empty, type="email" rejects text without an @, and minlength, min and max set limits.
<form>
<label for="e">Email</label>
<input id="e" type="email" required>
<button type="submit">Subscribe</button>
</form>12Problem
Does the server still check?
The browser checks required fields. Does the server still need to check?
Hint
Anyone can send data without using your page.
Solution
Answer: Yes, browser checks can be bypassed
Built-in checks help the visitor; the server protects the data.
13Practice
Label the sign-up form
Give each input an id and a name (name and email), and replace the bare words with <label for="..."> elements. Make the email field type="email" and required, and the button type="submit".
- label for name
- input id="name" name="name"
- label for email
- required email input
- submit button
<form action="/subscribe" method="post">
Name <input type="text" placeholder="Name">
Email <input type="text" placeholder="Email">
<button>Subscribe</button>
</form>Hint
Each label's for equals its input's id. Add required and type="email" to the email field.
A solution
<form action="/subscribe" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<button type="submit">Subscribe</button>
</form>14Practice
A group of radio buttons
Wrap the choices in a <fieldset> with a <legend> that says Loaf size. Give both radio buttons name="size" and wrap each one, with its word, in a <label>.
- fieldset with legend
- two radios named size
- each radio in a label
<form>
<p>Loaf size</p>
<input type="radio" value="small"> Small
<input type="radio" value="large"> Large
</form>Hint
The question becomes the legend; both inputs get name="size".
A solution
<form>
<fieldset>
<legend>Loaf size</legend>
<label><input type="radio" name="size" value="small"> Small</label>
<label><input type="radio" name="size" value="large"> Large</label>
</fieldset>
</form>15Explain
Common mistakes
<input type="email" placeholder="Email"><label for="em">Email</label>
<input id="em" type="email">A placeholder disappears as soon as someone types, and many screen readers do not read it as a name. Give every field a label.
16
Summary
- form sends each control name and value to action, by get or post.
- Every control gets a label (for matches id); placeholder is not a label.
- Choose the right type; group radios with fieldset and legend; the server checks again.