HTML REFERENCE · 06
Forms
Forms collect information through labeled controls. HTML supplies input types, selection controls, and basic validation. A server is needed to process or store submitted information.
Forms at a glance
| Element or attribute | What it means | When to use it |
|---|---|---|
form |
A group of controls that can submit values together. | Use when collecting a search, request, or other input. Do not nest forms. |
action / method |
Action gives the submission destination. Method selects how values are sent. | Use GET for retrieval, such as a search; values appear in the URL. Use POST for a request body when changing or submitting records. POST alone does not encrypt data; HTTPS protects transport. |
label / for / id |
Label provides a control’s visible name; for matches that control’s id. | Label each input so people know what to enter. Unique IDs make the association unambiguous. |
input / type |
A void element whose type selects a control, such as text, email, number, checkbox, or radio. | Choose a type that matches the expected information. Email provides basic format checking, not proof that an address exists. |
name / value |
Name is the key sent with a control; value is its submitted or initial value, depending on the control. | Give submitted controls names. For radio buttons and checkboxes, set an explicit value to identify the selected choice. |
required |
A boolean attribute making a supported control mandatory for submission. | Use for genuinely necessary fields. An empty required text control fails browser validation. |
placeholder |
A short hint inside an empty text control. | Use for an example format. It disappears during typing and does not replace a label. |
fieldset / legend |
A group of related controls and its caption. | Use for a radio group or related set of checkboxes. Put legend first inside fieldset. |
checked |
A boolean attribute setting an initial checkbox or radio selection. | Use for an appropriate default. Radios with the same name form a single-choice group; checkboxes allow independent choices. |
select / option / selected |
A selection control, its choices, and an initial chosen option. | Use for a compact list of choices. Give options explicit values; selected identifies the initial choice. |
textarea / rows / cols |
A multiline text control and its initial visible dimensions. | Use for longer messages. Initial text goes between its tags, rather than in a value attribute. Rows and cols affect size, not maximum length. |
min / max / maxlength |
Constraints on supported inputs: a lower value, an upper value, or text length. | Use min and max with number or date inputs, and maxlength for text controls. The server must also validate submitted values. |
button / type |
A button whose type specifies whether it submits, resets, or has no default action. | Set type explicitly: submit sends the form, button has no built-in action, and reset restores initial values. |
disabled / readonly |
Disabled makes a control unavailable; readonly prevents editing on supported text controls. | Disabled controls are not submitted. Readonly controls can still be focused and submitted. Both are boolean attributes. |
Connecting a label and an input
For and id connect the label to the control. Name supplies the submission key, while placeholder is only a hint. Required and type="email" enable browser checks when this control belongs to a submitted form.
Code
<p>
<label for="contact-email">Email address</label>
<input type="email" id="contact-email" name="email"
placeholder="reader@example.com" required>
</p>
Output
This isolated control demonstrates labeling and typing. It has no form submission button. Never use placeholder text as the only label.
Grouping related choices
The two radio inputs share name="pace", so only one can be selected at a time. The checkbox is independent. Legend describes the whole group; each label names one control.
Code
<fieldset>
<legend>Walking preferences</legend>
<p>
<input type="radio" id="pace-easy" name="pace" value="easy" checked>
<label for="pace-easy">Easy pace</label>
</p>
<p>
<input type="radio" id="pace-steady" name="pace" value="steady">
<label for="pace-steady">Steady pace</label>
</p>
<p>
<input type="checkbox" id="view-stops" name="stops" value="views">
<label for="view-stops">Include viewpoint stops</label>
</p>
</fieldset>
Output
Only checked radio buttons and checkboxes contribute values when submitted. A checkbox left unchecked is omitted rather than sent as “false.”
Choosing from a list and writing a message
Select offers a finite list. Textarea accepts several lines. Each has a visible label, a unique ID, and a name for use in a form.
Code
<p>
<label for="route-choice">Route</label>
<select id="route-choice" name="route">
<option value="ridge" selected>Ridge Loop</option>
<option value="valley">Valley Path</option>
</select>
</p>
<p><label for="route-notes">Route notes</label></p>
<textarea id="route-notes" name="notes" rows="3" cols="20"
maxlength="200">I prefer a quiet route.</textarea>
Output
Constraining input and showing availability
Min and max restrict valid numbers. Readonly displays a fixed text value that can still be submitted. Disabled makes the button unavailable; remove the attribute to enable it.
Code
<p>
<label for="walk-count">Number of walks (1 to 5)</label>
<input type="number" id="walk-count" name="walks" min="1" max="5" value="2">
</p>
<p>
<label for="area-name">Area</label>
<input type="text" id="area-name" name="area" value="North valley" readonly>
</p>
<button type="submit" disabled>Booking unavailable</button>
Output
A number constraint is checked during form submission; it does not guarantee that every browser prevents typing an out-of-range value. Browser checks improve usability, but servers must check data too.
Understanding a GET submission
This complete demonstration submits a search term back to this same lesson. Action="index.html" resolves to this folder’s HTML file. Method="get" puts the named value in the URL, such as ?q=ridge.
Code
<form action="index.html" method="get">
<p>
<label for="demo-search">Example search term</label>
<input type="text" id="demo-search" name="q" value="ridge" required>
</p>
<button type="submit">Show GET submission in URL</button>
</form>
Output
Submitting reloads this lesson with a query string. This static page does not search, save, or process the term. A working search would need a suitable processing endpoint. Avoid putting private information into a GET query.
Further reading: W3Schools: Forms. The explanations and examples here are written for this guide.
Study questions
Answer in your own words before opening each explanation.
How do for and id work together?
A label’s for value matches the control’s unique id. Activating the label then focuses or activates its associated control.
How does name differ from id?
Id identifies the element in the document. Name identifies its value in submitted form data. An id alone does not provide a submission key.
Does placeholder replace a label?
No. Placeholder is a temporary hint that disappears while typing. A visible label should remain available.
How do radios differ from checkboxes?
Radios sharing a name allow a single choice from a group. Checkboxes allow independent selections.
What does an unchecked checkbox submit?
Nothing for that checkbox. It is omitted from the form data, rather than automatically submitting false.
Where do textarea defaults go?
Between the opening and closing textarea tags. A value attribute does not set textarea’s initial content.
How do GET and POST differ?
GET puts submitted values in the request URL and suits retrieval. POST puts values in the request body and commonly submits changes. Neither replaces HTTPS for transport security.
Does required guarantee valid data on the server?
No. It triggers a browser check for supported controls. A server must validate requests independently because browser checks can be bypassed.
How do disabled and readonly differ?
Disabled controls are unavailable and omitted from submission. Supported readonly controls prevent editing but remain focusable and can be submitted.
Does this lesson’s search form perform a search?
No. It demonstrates a real GET submission by reloading this file with a query string. Processing the query requires additional server behavior.