Sub topic 2
How Names and Values Become Submitted Data
Assume you know basic HTML and can inspect a page in browser developer tools. A form submission is a set of name-value pairs. A control contributes data only when it has a name and is successful, such as being enabled and selected.
Names and values
The name identifies a field. The value is the data sent for that field. The id connects a control to its label, but it does not determine submitted data.
<form action="/signup" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" value="[email protected]">
<label for="age">Age</label>
<input id="age" name="age" type="number" min="13">
<button type="submit">Create account</button>
</form>If the user submits this form, the request includes pairs such as email=sam%40example.com and age=.... An input without name is not submitted. A disabled control is not submitted either.
Choosing the right input type
Use the type that describes the data. Browsers can provide suitable keyboards, built-in validation, and accessibility hints.
| Data | Suitable control |
|---|---|
| Email address | `input type="email"` |
| Whole-number quantity | `input type="number"` |
| Date | `input type="date"` |
| Longer free text | `textarea` |
The type improves the browser experience, but the server must validate the submitted value too.
Checkboxes, radio buttons, and select menus
Radio buttons with the same name form one choice group. Only the selected radio contributes a pair. A checkbox contributes its value only when checked. An unchecked checkbox contributes nothing.
A select submits the selected option's value, not necessarily its visible text. If an option has no value, its text is used.
GET versus POST
GET places the pairs in the URL, for example /search?term=forms. It suits safe, repeatable requests such as searches and filters. URLs can be bookmarked and logged, so do not put passwords or private data in a GET request.
POST places the data in the request body. It suits actions that create or change server data, such as registration. POST is not encryption; use HTTPS to protect data in transit.
Exercise
A checked checkbox has name="updates" and value="yes". What pair does it submit, and what happens when it is unchecked?
Show answer
It submits updates=yes when checked. It submits no pair when unchecked.