Forms
Forms are how a web page collects information from a visitor — search boxes, login screens, contact forms, checkout flows. This chapter covers the foundational structure; Intermediate Course Chapter 1 goes much deeper into validation, fieldsets, and the full range of input types.
The Basic Form Structure
- action — where the form's data gets sent when submitted.
- method — how it's sent:
getappends data to the URL (visible, bookmarkable, size-limited);postsends it in the request body (not visible in the URL, no practical size limit).
label and for — The Most Important Form Accessibility Pairing
The for attribute on a <label> must exactly match the id of its corresponding input — this creates a genuine, browser-recognised association, not just visual proximity.
Why this matters beyond just looking right
- Screen readers announce the label when the input receives focus — without the for/id link, a sighted user sees the label, but it may be completely disconnected for a screen reader user.
- Clicking the label text itself focuses or activates the associated input — genuinely useful on mobile, where tapping a small checkbox/radio button precisely is harder than tapping its larger text label.
<label>Username <input type="text" name="username"></label> creates the same accessible association implicitly, through nesting rather than matching id/for values — a valid alternative, though the explicit for/id pairing is generally considered clearer to read in larger forms.
Common Input Types
name — The Attribute That Actually Submits Data
Easy to overlook: an input needs a name attribute for its value to actually be included when the form is submitted — id alone (used for the label association above) is not enough on its own.
Radio Buttons — Grouped by a Shared name
Both radio buttons share name="size" — that shared name is what makes them mutually exclusive (selecting one deselects the other); each still needs its own unique id for its label to work correctly.
Chapter 7 Quick Reference
- <form action="..." method="..."> — where data goes, and how it's sent (GET vs POST)
- Never use GET for sensitive data — it ends up visible in the URL
- label for="x" + input id="x" — the core accessibility pairing; alternatively, nest the input inside the label
- name attribute — required for an input's value to actually be submitted; id alone isn't enough
- Radio buttons — grouped into a mutually exclusive set via a shared name, with unique ids per option
- Common types: text, email, password, number, checkbox, radio, submit — full range covered in Intermediate Ch1
- Next chapter: semantic HTML — header, nav, main, article, section, footer