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
nameattribute 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
<!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 parts
| Part | Job |
|---|---|
<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"
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
- Select the example and choose Try it. Fill in the form and press Sign up. Read the message.
- Remove
name="email"from the email field and submit again. The email is missing from the data. - Click the word "Email" in the preview. The cursor moves into the box.
Common mistakes
- No
nameattribute. The field looks fine but its value is never sent. Fix: every field that should be sent needs aname. forandiddo 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 outaction, as here.
Check yourself
- What happens to a field that has no
nameattribute? - Which two attributes connect a label to its field?
- You submit the example with name "Ana". What appears at the end of the address?
Answers
- It is not sent.
foron the<label>andidon the field — with the same value.?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.


