Forms, Labels and Submission

Beginner · 4 min

How a form turns controls into a request: action, method and name, which controls get submitted, and why every control needs a label.

Learndev · 7 min

Forms, Labels and Submission

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by LearndevReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `<form>` collects its controls and sends them to the URL in `action` using `method`. `GET` puts the data in the query string; `POST` puts it in the request body.
  • Only controls with a `name` are submitted. In a test, an input with no `name`, a disabled input, and an unchecked checkbox were all left out of the request.
  • Every control needs a label. A `<label for="id">` or a wrapping `<label>` ties the text to the control, names it for screen readers, and makes the text clickable. A placeholder is not a label.
  • The `for` value must match the control's `id`. A label whose `for` points at nothing labels nothing: in a test the control had zero labels.
  • The browser encodes the data for you: spaces become `+`, `&` becomes `%26`, and a `<textarea>` newline becomes `%0D%0A`.

Why forms exist

A page that only displays content is a one-way street. The moment you want something back, such as a search term, a sign-up or a message, the browser needs a standard way to collect input and send it to a server. That is a form. It bundles a set of controls, knows where to send them and how, and does the encoding for you.

Without a form, you would need JavaScript to read each field, build a request and send it. With one, a few attributes do it, and it keeps working if the script fails to load. The frontend forms lesson covers form handling in a modern app; this one is the HTML underneath.

The form element

<form action="/subscribe" method="post">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>
  <button>Subscribe</button>
</form>

action is the URL that receives the data. If you leave it out, the form posts to the current page. method is get or post; the default is get.

GET puts the fields in the URL: /search?q=html+forms. That makes the result bookmarkable, shareable and cacheable, so it fits reading actions like search. POST puts them in the request body, out of the address bar, and signals that the request changes something on the server. Use it for sign-ups, comments and anything with side effects. The HTTP messages lesson covers methods in more depth.

What gets submitted

The form’s job on submit is to build a list of name and value pairs. Not every control makes the list, and the rules surprise people. I tested a form with a mix of controls, submitted it in headless Chromium to a local server and read the request:

text=a+b%26c&cbOn=on&cbVal=yes&r=2&sel=Plain+text&multi=a&multi=b&ta=line1%0D%0Aline2&up=&btn=pressed
  • Names are required. An input with no name was left out. A disabled input and a control inside a disabled <fieldset> were also left out.
  • Unchecked checkboxes vanish. The checked checkbox with no value was sent as cbOn=on; the unchecked one was missing, so the server cannot tell “unchecked” from “field not on the form”.
  • Radios send one value: only the checked one, r=2.
  • A <select> sends the option’s value, or its text if there is no value. The option with no value was sent as Plain+text. A multiple select sent one pair per selected option.
  • A <textarea> newline becomes CRLF, shown as %0D%0A.
  • An empty file input still sends a name: up= with nothing after it.
  • <output> is not submitted. It displays a result and takes no part in the request.
  • A button’s name and value are sent only when that button was used to submit.
  • Encoding is automatic. A space became + and & became %26.

With method="get", the same pairs went into the URL; with post, they went in the body with Content-Type: application/x-www-form-urlencoded. The default encoding cannot carry a file’s contents. File inputs covers multipart/form-data, which does.

Labels

Every control needs a label that says what it is for. The label does three jobs:

  1. Gives the control its accessible name. A screen reader announces “Email, edit text” instead of “edit text”.
  2. Makes the text a bigger click target. In a test, clicking a <label for="a"> moved focus to the input, and clicking a label that wrapped a checkbox toggled it.
  3. Keeps the cue visible. Unlike a placeholder, it stays when the user types.

There are two ways to write one. The explicit form uses for and id: <label for="email"> and <input id="email">. The implicit form wraps the control: <label>Email <input name="email"></label>. Both work. The explicit form is easier to style because the label and control are separate elements.

The for value has to match an id exactly. In a test, a label whose for pointed at an id that did not exist left the control with zero labels (input.labels.length was 0). A control with two labels pointing at it had two. Nothing warns you about a typo here, so check it by clicking the label.

Placeholders are not labels. They vanish on input, default to low contrast, and are not a dependable accessible name. Use one for an example value such as name@example.com, never for the field’s name. The accessible names lesson covers how the browser decides what a control is called.

Enter and the submit button

A form with a submit button submits when the user presses Enter in a text field. A form with one text field and no button also submitted in a test; a form with two text fields and no button did not. That is the specified rule (“implicit submission”), and it means a form with two fields and only a type="button" button will not submit on Enter. Buttons and submit behavior covers the rest.

What the video covers

The embed is a clip, 0:00 to 3:46, from Learndev’s “HTML Forms and Inputs in 7 Minutes!” (May 2024). The chapter list covers HTML form basics, a login form, input labels, and placeholders before it moves on to a signup form, radio buttons, checkboxes, date inputs and fieldsets. The clip stops at the signup form, since radios and fieldsets belong to later lessons, and fieldset and legend reuses the later part of the same video. I could not read a transcript, so I do not describe the demos. Everything in the submitted-data list above comes from my own test in Chromium; Firefox and WebKit were not run for that test, and a separate file-input check produced the same result in all three.

Where this fits

This is the first lesson in the forms group. Input types covers what goes inside the form. Labels return in accessible names and descriptions, and forms get a second look in HTML validation and debugging.

Check yourself

  1. 01A form has `<input type="text" id="email">` with no `name`. The user types a value and presses submit. What does the server receive for that field?

    Nothing. A control is only part of the submission if it has a `name`. The `id` is for labels, CSS and JavaScript, and the form never reads it when building the request.

  2. 02What is the difference between `method="get"` and `method="post"` for a search form and a sign-up form?

    With `GET` the fields go into the URL, such as `?q=html+forms`, which suits a search because the result page can be bookmarked and shared. With `POST` they go in the request body, which suits a sign-up because the data is not stored in the address bar or history and the request changes server state.

  3. 03You write `<label for="mail">Email</label><input id="email" name="email">`. What is wrong?

    The `for` value is `mail` but the input's `id` is `email`, so the label points at nothing. Clicking the label does not focus the field and screen readers do not announce "Email" for it. Fix it by making the `for` and `id` match.

  4. 04Why is `placeholder="Email"` not a replacement for a label?

    The placeholder disappears as soon as the user types, so they lose the cue for what the field is. It is also low-contrast by default and is not reliably exposed as the control's name. Use a visible `<label>` and, if you want a hint, a placeholder as an example value.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark