Sub topic 14
A first look at form controls
This chapter assumes you can write a complete HTML document, nest elements validly, and use attributes. It does not cover CSS, JavaScript, or server-side code. It covers what the browser does with a form and how to mark up the common controls correctly.
What a form does
A form is a group of controls that a user fills in and the browser sends to a URL. The form element defines where the data goes and how it travels. Controls inside it supply the data.
actionis the URL that receives the data. If you omit it, the data goes to the current page's URL.methodisgetorpost.getis the default.
With get, the browser appends the data to the URL as a query string, such as /search?q=html. Use it for searches and other requests that change nothing. With post, the browser sends the data in the request body. Use it when the request changes something on the server or carries data you do not want in the URL.
The name attribute decides what is sent
The browser sends each control's name together with its current value. A control with no name is not submitted at all. This is the most common reason a field arrives empty on the server.
The id attribute does a different job. It identifies the element on the page so that a label, a fragment link, or a script can refer to it. A control usually needs both: name for the server and id for the label.
Labels
Every control needs a visible label that is programmatically tied to it. Use the label element. Clicking the label focuses the control, and screen readers announce the label when the control gets focus.
There are two ways to connect them:
- Explicit:
<label for="email">matches<input id="email">. - Implicit: the control is nested inside the
labelelement.
The explicit form is the one used in this tutorial. The for value must equal the control's id exactly. Each id must be unique in the document.
The input element and its type
input is a void element, so it has no end tag. Its type attribute changes what the control looks like and how it validates. If you omit type or give an unknown value, the browser treats it as text.
| Type | Use | Notes |
|---|---|---|
| `text` | Single line of free text | The default |
| `email` | Email address | Checks for a plausible address format |
| `password` | Secret text | Masks the characters on screen |
| `number` | Numeric value | Supports `min`, `max`, `step` |
| `date` | Calendar date | Value is sent as `YYYY-MM-DD` |
| `checkbox` | On/off choice | Sent only when checked |
| `radio` | One choice from a group | Group by sharing a `name` |
| `hidden` | Value the user does not see | Not a security measure |
Using the right type gives users a suitable keyboard on phones and gives you free validation. It does not replace server-side checks, because anyone can send a request without using your page.
Buttons
Use the button element for buttons. Its type can be:
submit: sends the form. This is the default inside a form.reset: restores every control to its initial value. Users rarely want this.button: does nothing by itself and is meant for scripts.
Write type="submit" explicitly so the intent is clear. A button that is meant for scripting but has no type will submit the form by accident.
Text areas, selects, and option groups
Use textarea for multi-line text. It has start and end tags, and the content between them is the initial value. Use rows and cols for a starting size.
Use select with option children for a drop-down list. Each option has a value that is sent, and its text is what the user sees. If you omit value, the text is sent instead.
Radio buttons and checkboxes
Radio buttons that share a name form a group, and the browser lets the user pick only one. Each radio needs its own value so the server can tell which was chosen. Add checked to preselect one.
A checkbox is sent only when checked, and its value defaults to the string on. Give it a meaningful value.
Wrap related controls in fieldset and name the group with legend. Screen readers announce the legend before each control in the group, which gives radio buttons the context for their individual labels.
A complete example
This page uses every control from the sections above. Save it as signup.html and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Course sign-up</title>
</head>
<body>
<h1>Course sign-up</h1>
<form action="/signup" method="post">
<p>
<label for="name">Full name</label>
<input id="name" name="name" type="text" required>
</p>
<p>
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
</p>
<p>
<label for="start">Start date</label>
<input id="start" name="start" type="date">
</p>
<p>
<label for="track">Track</label>
<select id="track" name="track">
<option value="web">Web basics</option>
<option value="data">Data basics</option>
</select>
</p>
<fieldset>
<legend>Preferred contact method</legend>
<p>
<input id="contact-email" name="contact" type="radio" value="email" checked>
<label for="contact-email">Email</label>
</p>
<p>
<input id="contact-phone" name="contact" type="radio" value="phone">
<label for="contact-phone">Phone</label>
</p>
</fieldset>
<p>
<label for="notes">Notes</label><br>
<textarea id="notes" name="notes" rows="4" cols="40"></textarea>
</p>
<p>
<input id="terms" name="terms" type="checkbox" value="yes" required>
<label for="terms">I accept the terms</label>
</p>
<button type="submit">Sign up</button>
</form>
</body>
</html>Notice the structure. Each control sits in a p so the browser lays them out on separate lines. The two radio buttons share name="contact" but have different id and value values. Submitting with the default choices would send contact=email.
Because /signup probably does not exist on your machine, submitting will show an error page or a "not found" message. The browser still built and sent the request. To see exactly what was sent, open the developer tools, go to the Network panel, and submit again.
Built-in validation
Constraint attributes let the browser check input before sending. The common ones are:
required: the field must not be empty, or the checkbox must be checked.minlengthandmaxlength: limits on text length.minandmax: limits onnumberanddatevalues.pattern: a regular expression the whole value must match.
When a check fails, the browser blocks submission and shows a message at the first invalid control. This is a convenience for users. It is not security, because the checks live in the client.
Common errors
- A missing
namemeans the control's value is never sent. - A
forvalue that does not match anyidleaves the label unattached. Clicking it does nothing. - Radio buttons with different
namevalues are not grouped, so several can be selected at once. - Duplicate
idvalues break label association and fragment links. - Using a link styled as a button, or a
div, in place ofbuttonloses keyboard and form behavior. - Putting a form inside another form is invalid, and browsers ignore the inner one.
- Using
getfor a password form places the password in the URL and in browser history.
Where this leads
This chapter covers the basic controls, which is enough to build working forms. The final chapter returns to forms in its accessibility checks, where labels, logical order, and validation of your markup all come together.
Exercise
A form has two radio buttons written as <input type="radio" name="size" value="s"> and <input type="radio" name="sizes" value="m">. The user picks both, and the server receives two values. What is wrong, and how do you fix it?
Show answer
The radio buttons have different name values (size and sizes), so the browser does not treat them as one group and lets both be selected. Give them the same name, for example name="size" on both, and keep distinct value attributes.
Exercise
A page has <label for="mail">Email</label> and <input id="email" type="email" required>. Clicking the label does nothing, and the server never receives the address. List the two problems and the corrected input element.
Show answer
The for value (mail) does not match the input's id (email), so the label is not attached. The input also has no name attribute, so its value is not submitted. Fix: change the label to for="email" and use <input id="email" name="email" type="email" required>.
Exercise
Why should a signup form use method="post" and not method="get" when it includes a password field?
Show answer
With get, the browser puts the field values in the URL's query string, where they appear in browser history, server logs, and shared links. With post, the values are sent in the request body instead.