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 introduces the form element and a few basic controls. It does not cover validation rules, styling, or server-side handling in depth.
What a form does
A form collects input from a user and sends it somewhere. The form element wraps the controls. Two attributes decide where and how the data travels:
actionis the URL that receives the data.methodis the HTTP method.getputs the data in the URL.postputs it in the request body.
Each control that should be sent needs a name attribute. The browser submits pairs of name=value. A control with no name is not sent.
The first form
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<button type="submit">Subscribe</button>
</form>Three elements do the work here:
labelis the visible text for a control.inputis a void element, so it has no end tag. Itstypeattribute picks the kind of control.buttonwithtype="submit"sends the form.
Connecting a label to its input
The for attribute on the label must match the id on the input. The two values are the same string, email, in the example above. This link has practical effects:
- Clicking the label moves focus to the input.
- Screen readers announce the label when the input gets focus.
- The clickable area is larger, which helps on touch screens.
Placeholder text is not a substitute for a label. It disappears when the user types, and many screen readers do not treat it as a name.
Common input types
The type attribute changes the keyboard on mobile devices, the built-in checks, and the appearance.
| Type | Use |
|---|---|
| `text` | Single line of free text (the default) |
| `email` | An email address, checked for basic format |
| `password` | Text that is masked as you type |
| `number` | A numeric value |
| `checkbox` | An on/off choice |
| `radio` | One choice from a group |
For several lines of text, use textarea. For a dropdown, use select with option children.
A slightly larger form
<form action="/signup" method="post">
<p>
<label for="name">Name</label>
<input id="name" name="name" type="text" required>
</p>
<p>
<label for="bio">About you</label>
<textarea id="bio" name="bio" rows="4" cols="40"></textarea>
</p>
<fieldset>
<legend>Preferred contact</legend>
<label><input type="radio" name="contact" value="email" checked> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
<p>
<label><input type="checkbox" name="news" value="yes"> Send me the newsletter</label>
</p>
<button type="submit">Sign up</button>
</form>Points to notice:
- Radio buttons in one group share the same
name. That is what makes them mutually exclusive. Each has its ownvalue. - A label can wrap its input. In that case
forandidare not needed. fieldsetgroups related controls, andlegendnames the group.textareais not void. Its content is its starting text.requiredandcheckedare boolean attributes. Their presence means true.
Common errors
- Missing
name. The control appears on the page but its value never arrives at the server. - Mismatched
forandid. The label looks right but is not linked. Clicking it does nothing. - Duplicate
idvalues. Anidmust be unique in the document. A duplicate breaks label links. - Different
namevalues in a radio group. The user can select several options. - Using a
divor link as a button. Usebutton. It is focusable and works with the keyboard by default. - Omitting
typeon a button. Inside a form, a button defaults to submit.
Exercises
Exercise
A text input has id="city" and name="town". A label has for="town". What two problems exist, and how do you fix them?
Show answer
The label's for value must match the input's id, so the label is not linked. Change it to for="city". Also, the name "town" is what the server receives, which may be fine, but it should be chosen deliberately. The linking problem is the one that must be fixed.
Exercise
Two radio buttons offer "Email" and "Phone", but a user can select both. What is wrong?
Show answer
The two inputs have different name attributes. Give them the same name, such as name="contact", and keep distinct value attributes.