Lesson 35 of 51 · Module 6, lesson 1 of 6 · 4 min

How forms work

Build a form with labels, fields and a button, and see the data it sends — without a server.

What you'll learn

  • The four parts of every form: <form>, <label>, <input> and a submit button
  • What the name attribute does
  • How to see the data your form sends, without a server

Why this matters

Forms are how visitors talk back: sign-up, contact, search, checkout. Every form uses the same few parts. Get them right, and your form works for everyone — including people who use a keyboard or a screen reader.

A complete form

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Newsletter</title>
</head>
<body>
  <h1>Get new recipes by email</h1>
  <form>
    <p>
      <label for="name">Your name</label>
      <input id="name" name="name" type="text">
    </p>
    <p>
      <label for="email">Email</label>
      <input id="email" name="email" type="email">
    </p>
    <p>
      <button type="submit">Sign up</button>
    </p>
  </form>
</body>
</html>

What you'll see: two boxes, each with its label, and a Sign up button.

The page "Get new recipes by email" with a text box labelled "Your name", a text box labelled "Email", and a "Sign up" button.

The parts

PartJob
<form>Holds all the fields. When the form is submitted, it sends every field inside it.
<label for="…">The field's visible name. for must match the field's id.
<input>A box to type in. It is a void element. type says what kind of box (next lesson).
name="…"The name the computer uses for this field's value. Without it, the value is not sent.
<button type="submit">Sends the form.

Labels matter

A label connected with for and id does two things:

  • Screen readers read the label when the field is selected ("Email, edit text").
  • Clicking the label puts the cursor in the field — a bigger target for everyone.

Never use placeholder text inside the box instead of a label. It disappears when you type, and many screen readers skip it.

What happens when you press "Sign up"

A form with "Your name" (name="name") containing Ana, "Email" (name="email") containing ana@example.com, and a Sign up submit button. After submitting with method get, the address ends with ?name=Ana&email=ana%40example.com. Each field becomes a name=value pair; the name attribute is the label the computer sees, and the label element is the one the person sees. A field with no name is not sent.

This form has no address to send to, so the browser sends it to the same page and adds the data to the end of the address:

index.html?name=Ana&email=ana%40example.com
  • ? starts the data.
  • Each field is name=value.
  • & separates the fields.
  • Special characters are coded: @ becomes %40, a space becomes +.

Try it on your own computer: build the form in my-website, open it, fill it in and press Sign up. Look at the address bar. This is exactly what a web server would receive. (In the Try code playground, a message shows the same data, because the playground does not change pages.)

Where real data goes — a server, an email service — is covered in Where form data goes.

Try it yourself

  1. Select the example and choose Try it. Fill in the form and press Sign up. Read the message.
  2. Remove name="email" from the email field and submit again. The email is missing from the data.
  3. Click the word "Email" in the preview. The cursor moves into the box.

Common mistakes

  • No name attribute. The field looks fine but its value is never sent. Fix: every field that should be sent needs a name.
  • for and id do not match. The label is not connected. How to spot it: clicking the label does nothing. Fix: make them identical.
  • Placeholder instead of a label. Fix: always a real <label>.
  • Fields outside the <form>. They are not sent. Fix: put them inside.
  • A form that posts to a server path, such as action="/register", when you open the file from your computer. You get a "file not found" page. Fix: while learning, leave out action, as here.

Check yourself

  1. What happens to a field that has no name attribute?
  2. Which two attributes connect a label to its field?
  3. You submit the example with name "Ana". What appears at the end of the address?

Answers

  1. It is not sent.
  2. for on the <label> and id on the field — with the same value.
  3. ?name=Ana&email=… (the name and the email, as name=value pairs).

Next: type="text" and type="email" are only two kinds of box. The next lesson shows how to choose the right one — and why it matters on phones.

Chat with us on WhatsApp