edubun
English
1 / 16

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 questions

You 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.

Your code
<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>
Preview

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.

type
<label for="f">Your answer</label>
<input id="f" type="text">
Preview

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

Which input type shows an email keyboard on phones and checks for @?

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.

  1. Give the input an id, here pc. It must be unique on the page.
  2. Write the label with the same value in for: for="pc". Clicking the word Postcode now puts the cursor in the field.
  3. Give it a name: postcode. That is the name the browser sends together with the value.
  4. Add required, so the form cannot be sent while the field is empty, and autocomplete so the browser can offer a saved postcode.
Your code
<label for="pc">Postcode</label>
<input id="pc" name="postcode" type="text" autocomplete="postal-code" required>
Preview

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.

Your code
<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>
Preview

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.

Your code
<form>
  <label for="e">Email</label>
  <input id="e" type="email" required>
  <button type="submit">Subscribe</button>
</form>
Preview

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".

Checks
  • label for name
  • input id="name" name="name"
  • label for email
  • required email input
  • submit button
Your code
<form action="/subscribe" method="post">
  Name <input type="text" placeholder="Name">
  Email <input type="text" placeholder="Email">
  <button>Subscribe</button>
</form>
Preview
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>.

Checks
  • fieldset with legend
  • two radios named size
  • each radio in a label
Your code
<form>
  <p>Loaf size</p>
  <input type="radio" value="small"> Small
  <input type="radio" value="large"> Large
</form>
Preview
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

Mistake
<input type="email" placeholder="Email">
Fixed
<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.

Next unitSemantic page structure
Full text · The whole lesson as one text

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. post sends it in the body of the request, which suits anything that changes data, such as an order. HTML builds the form; a program on the server decides what to do with what arrives.

<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>
Each label is tied to its input by matching for and id values.

Every control needs a label

The <label> element names a control. Connect the two by giving the input an id and the label a matching for attribute, or by putting the input inside the label. The connection does two things. Screen readers read the label when the field gets focus, so the user hears Email, edit text instead of just edit text. And clicking the label focuses the field or ticks the checkbox, which gives everyone, especially people using a phone, a bigger target. A placeholder (grey hint text inside the box) is not a label: it disappears as soon as you type, it is often low in contrast, and some assistive technology ignores it.

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. password hides what you type, date offers a calendar, and checkbox and radio are boxes to tick. For longer text, use <textarea>, and for a choice from a list, <select> with <option> elements inside.

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.

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. These checks help the visitor fix mistakes early, but they run in the visitor's browser and can be bypassed, so the server must check the data again. The autocomplete attribute, with values such as email or name, lets the browser fill in saved details, which saves typing for everyone and especially helps people with motor or memory difficulties.

In the editor below the form is shown in a sandbox, so pressing Subscribe sends nothing anywhere. The Check button only looks at your HTML.
Key points
  • 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.

Practice questions from Forms basics on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (9)
  • WHATWG, HTML Living Standard (html.spec.whatwg.org, checked 2026-10-17)
  • MDN Web Docs, Learn web development: Structuring content with HTML, and the HTML element reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content and developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements, checked 2026-10-17)
  • MDN, HTML images (alt text guidance, checked 2026-10-17)
  • W3C WAI, Images tutorial and alt decision tree (w3.org/WAI/tutorials/images/decision-tree, checked 2026-10-17)
  • CERN, A short history of the Web (home.cern/science/computing/the-birth-of-the-web/short-history-web/, checked 2026-10-17)
  • W3C and WHATWG memorandum of understanding, 28 May 2019 (w3.org/2019/04/WHATWG-W3C-MOU.html, checked 2026-10-17)
  • W3C, Using ARIA, first rule of ARIA use (w3.org/TR/using-aria/, checked 2026-10-17)
  • W3C Markup Validation Service (validator.w3.org, checked 2026-10-17)
  • MDN, Viewport meta element (checked 2026-10-17).

Checked: 2026-10-17