Web Development Pro | Lesson 007 | Forms with Labels and Useful Controls
Web Development Pro · Lesson 07 of 60
Module 1: HTML foundations
In this lesson
Prerequisites and learning outcomes
Complete lesson 6 first. This lesson builds on form names, types, grouping, and submission. By the end, you can build a labeled practice form and explain what data would be submitted. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
A form is an interface and a data contract
The workshop site needs a way to express a session preference. A form has two audiences: the person completing it and the system that receives named values. A visible label helps the person understand a control. A name attribute gives the submitted value a key. An id connects elements inside the document. These are related responsibilities, but they are not interchangeable.
This lesson's form is a local demonstration using fictional choices only. It navigates to a supplied preview page; it does not reserve a seat or send information to an organizer. Do not enter real contact details. Building a visible form is not the same as building a delivery service, and honest interface wording should make that boundary clear.
Connect labels explicitly
A label's for value must match the control's unique id. Clicking the label generally focuses or activates its control, giving users a larger target and a programmatic name. Placeholder text is not a replacement: it can disappear during typing and may not provide the same usable labeling. Keep an always-visible label even when an example placeholder is helpful.
<form action="preview.html" method="get">
<p>Practice only. Use the fictional options below.</p>
<label for="session">Choose a session</label>
<select id="session" name="session">
<option value="bicycle">Bicycle basics</option>
<option value="mending">Mending clothes</option>
</select>
<fieldset>
<legend>Experience level</legend>
<label><input type="radio" name="level" value="new" checked> New to this skill</label>
<label><input type="radio" name="level" value="some"> Some experience</label>
</fieldset>
<label><input type="checkbox" name="notes" value="yes"> Include preparation notes</label>
<button type="submit">Open practice preview</button>
</form>
A wrapping label, used for the radio buttons and checkbox here, also associates the text with the nested input. Choose an explicit id/for connection when it makes the structure clearer. Do not nest multiple unrelated controls inside one label. A person should be able to tell exactly which control each label describes.
Choose controls by meaning
A select offers one option from a list. Radio buttons sharing one name form a single-choice group; their values distinguish the choices. Checkboxes represent independent on/off choices. Fieldset groups related controls, while legend supplies the group's question or topic. “Experience level” describes the whole group; the individual labels describe its options.
Input types can provide useful interfaces and basic constraints. An email input signals an email address; a number input suits a quantity with arithmetic meaning. A telephone number is usually text-like data, not a quantity you add or subtract, and may contain a leading plus sign. Choosing number just because a value contains digits can remove meaningful formatting or produce a poor interaction.
Use textarea for multiline text. Its initial value goes between opening and closing tags, unlike the value attribute on many input controls. A button inside a form may submit it unless you specify another type, so write type="button" for controls that only perform local actions and type="submit" for the intended submission trigger.
Understand the submitted entries
If the example uses Bicycle basics, New to this skill, and a checked notes box, its GET submission conceptually contains session=bicycle, level=new, and notes=yes. The visible labels are not the submitted keys. An unchecked checkbox contributes no entry. A control without name generally does not contribute an ordinary named entry either.
GET appends form data to the destination URL. It is suitable for retrieving or searching with non-sensitive parameters, but it exposes those parameters to histories and logs. POST sends data in a request body, but that does not automatically encrypt it or make the receiving service trustworthy. HTTPS and responsible server handling still matter. We will not transmit sensitive data in this course's practice forms.
Disabled controls are unavailable for interaction and are not submitted. Readonly applies only to certain text-like controls and has different behavior. Do not disable important information merely to make it look gray. If a field cannot currently be used, explain why and how the user can proceed.
Review the form as a conversation
Read each label and group legend in sequence. Does the form ask only for what the task requires? Can a new visitor understand the options without reading a long instruction page? A short form with clear choices usually creates less confusion than a large form that collects information “just in case.”
Inspect ids for uniqueness and labels for exact matching. Then test the intended submission in a browser when available and verify the resulting URL and destination message. The supplied preview explicitly says no registration occurred. Static markup inspection can verify relationships and attributes, but it cannot substitute for observing the browser's full interaction.
Worked practice
Worked exercise 1: Repair a label
Task: A label uses for="workshop", but the select has id="session". Worked answer: change the label to for="session" or consistently rename the control. Name="session" alone does not connect the label. Verify both the identifier match and the visible wording.
Worked exercise 2: Predict the entries
Task: The learner selects Mending clothes and Some experience, leaving notes unchecked. Worked answer: the ordinary named entries are session=mending and level=some. Notes is absent because the checkbox is unchecked. A receiving program must not assume every checkbox key is always present.
Worked exercise 3: Prevent accidental submission
Task: Add a future “Show help” button inside the form. Worked answer: give it type="button" so it does not become an unintended submit trigger. The actual help behavior will require suitable markup or later JavaScript; naming a button does not implement its action automatically.
Knowledge check
Question 1
Which attribute provides a submitted key? A) id. B) for. C) name. D) aria-describedby.
Check question 1: answer and explanation
Answer: C. Name identifies the form entry; id identifies an element in the document.
Question 2
What connects an explicit label to a control? A) Matching for and id values. B) Matching for and name values alone. C) Identical placeholder and label text. D) A nearby paragraph without an association.
Check question 2: answer and explanation
Answer: A. The exact identifier connection supplies the programmatic label relationship.
Question 3
What does an unchecked checkbox normally submit? A) Its key with the string false. B) No entry for that checkbox. C) Its key with an empty string. D) Its key with its checked-state label.
Check question 3: answer and explanation
Answer: B. Absence is a normal checkbox state a receiver must handle.
Question 4
Which element names a fieldset group? A) caption. B) A placeholder on the first input. C) legend. D) A label around the entire fieldset.
Check question 4: answer and explanation
Answer: C. Legend gives the related controls a group-level description.
Question 5
Does method="post" alone encrypt a form? A) Yes, because values leave the URL. B) Yes, if every field is required. C) Yes, if the action is a relative path. D) No.
Check question 5: answer and explanation
Answer: D. The method determines request semantics; transport protection requires HTTPS and appropriate handling.