Sub topic 5
How Names and Values Become Submitted Data
This chapter assumes you can write a basic <form> with action and method, and that you know what a text input and a submit button are. A little JavaScript helps for the last section. By the end, you should be able to predict the exact data a form sends, and to explain why a control is sometimes missing from it.
The core model: a list of name/value pairs
When a form is submitted, the browser builds an ordered list of pairs, each made of a name and a value. This list is called the entry list. The browser then encodes it and sends it. The server never sees your labels, your layout, or your element IDs. It sees only the pairs.
Each pair comes from one control. A control contributes only if it is "successful", which means it passes every one of these checks:
- It has a non-empty
nameattribute. - It is not
disabled, and it is not inside a disabledfieldset. - If it is a checkbox or radio button, it is checked.
- If it is a
select, at least one option is selected. - If it is a button, it is the one that triggered the submission.
Pairs are added in tree order, which is the order the controls appear in the HTML. CSS positioning does not change it.
A worked example
<form action="/signup" method="post">
<label for="user">Username</label>
<input id="user" name="username" type="text" value="ada">
<label for="bio">Bio</label>
<textarea id="bio" name="bio">Hello
world</textarea>
<input type="hidden" name="source" value="homepage">
<input type="text" value="not sent">
<input type="text" name="nickname" disabled value="also not sent">
<label><input type="checkbox" name="terms" checked> Accept terms</label>
<label><input type="checkbox" name="newsletter" value="yes"> Newsletter</label>
<button type="submit" name="action" value="create">Create</button>
<button type="submit" name="action" value="preview">Preview</button>
</form>If the user leaves the defaults and clicks "Create", the submitted pairs are:
| Name | Value | Why it is included |
|---|---|---|
| `username` | `ada` | Named, enabled text input |
| `bio` | `Hello` and a line break and `world` | Named textarea |
| `source` | `homepage` | Hidden inputs are always sent |
| `terms` | `on` | Checked, and no `value` attribute, so the default is `on` |
| `action` | `create` | Only the clicked button is sent |
The unnamed input, the disabled input, and the unchecked newsletter checkbox contribute nothing. The "Preview" button is also left out, because it was not the submitter.
Value rules by control type
Where the value comes from depends on the control.
- Text-like inputs and textarea: the current content. An empty field still sends a pair with an empty value, such as
username=. Textarea line breaks are normalized to CRLF (\r\n) on submission. - Checkbox and radio: the
valueattribute, or the stringonif there is none. Unchecked controls send nothing, so "absent" is the only way a server learns that a box was unchecked. - Select: the
valueof each selected option. If an option has novalueattribute, its text content is used instead. - Hidden input: its
value, exactly as written or as changed by script. - Buttons: the button's
nameandvalue, only when it submits the form. - File input: the file's name and contents. [File uploads are covered in their own chapter.]
A select with the multiple attribute sends one pair per selected option:
<select name="topics" multiple>
<option value="css" selected>CSS</option>
<option value="js" selected>JavaScript</option>
<option value="html">HTML</option>
</select>This submits topics=css&topics=js. Repeated names are legal, and you will rely on them for checkbox groups too.
Reusing a name
Names do not need to be unique, and the intended behavior differs by control type.
- Radio buttons must share a name. The shared name is what makes them a group where only one can be selected, and it is what produces a single pair.
- Checkboxes may share a name to form a list. Several checked boxes with
name="topic"produce severaltopic=...pairs. - Text inputs with the same name produce several pairs in document order. This is how a server receives a list of values, such as
tag=a&tag=b.
Some frameworks read repeated names as arrays, and some keep only the first or last value. Check how your server handles them before you rely on repetition. PHP, for example, expects names like topic[] to build an array.
How the pairs are encoded
With the default encoding, application/x-www-form-urlencoded, each name and value is percent-encoded. Spaces become +, and characters outside the safe set become %XX byte sequences in UTF-8. Pairs are joined with &, and each name is joined to its value with =.
For username=Ada Lovelace and bio=a&b, the body is:
username=Ada+Lovelace&bio=a%26bEncoding is why a user can type & or = without breaking the structure. You never need to escape values yourself. With method="get", the same string becomes the query part of the URL. [GET and POST are compared in a later chapter.] The enctype attribute can switch the encoding to multipart/form-data or text/plain. The names and values stay the same, and only the wire format changes.
Common errors
These problems account for most "my field never arrives" reports.
- Missing
name. Theidis for labels and scripts. Onlynamecreates a pair. This is the most common mistake. - Using
disabledto block editing. Disabled controls are not submitted. If the value must be sent but not edited, usereadonlyon a text input. Readonly controls are submitted, butreadonlydoes not apply to checkboxes, radios, or selects. - Expecting unchecked checkboxes to send
false. They send nothing. Treat a missing key as "off" on the server. - Unexpected
on. A checkbox withoutvaluesendson. Always set a meaningful value. - Duplicate names across radio groups. Two unrelated radio sets that share a name merge into one group.
- Overlooking the submitter. A button's name and value are sent only when that button is used. Pressing Enter in a text field triggers the form's default button, which is the first submit button in tree order. That button's pair is included.
- Controls outside the form element. A control placed elsewhere in the page can belong to a form through
form="form-id". Without that attribute, a control outside the<form>is not submitted.
Seeing the pairs in JavaScript
FormData builds the same entry list the browser would submit, which makes it a good debugging tool. Here it is on a full page you can open in a browser:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Entry list</title></head>
<body>
<form id="demo">
<input name="tag" value="a">
<input name="tag" value="b">
<label><input type="checkbox" name="terms"> Terms</label>
<button type="submit">Show</button>
</form>
<script>
document.getElementById('demo').addEventListener('submit', (event) => {
event.preventDefault();
const data = new FormData(event.target);
console.log([...data]);
console.log(Object.fromEntries(data));
console.log(data.getAll('tag'));
console.log(new URLSearchParams(data).toString());
});
</script>
</body>
</html>With the box unchecked, the console shows:
[["tag","a"],["tag","b"]]for the full entry list.{tag: "b"}forObject.fromEntries, which keeps only the last value for a repeated name.["a","b"]forgetAll.tag=a&tag=bfor the encoded string.
Use getAll whenever a name can repeat. Notice that terms is absent from every result, which matches the rules above. When a form is submitted by a button, FormData created inside the submit handler does not include that button unless you pass the submitter, as in new FormData(form, event.submitter). Support for that argument is recent, so check your target browsers.
Exercise
A form contains a text input with only an id="city", a checkbox name="agree" value="yes" that is not checked, a disabled input name="token" value="x", a readonly input name="plan" value="pro", and a submit button name="go" value="1" that the user clicks. Which name/value pairs are submitted?
Show answer
Only plan=pro and go=1. The city input has no name. The unchecked checkbox sends nothing. The disabled input is skipped. The readonly input is sent, and the clicked button contributes its name and value.
Exercise
A page has three checkboxes with name="topic" and values css, js, and html. The user checks css and html. What does the URL-encoded body look like, and what does Object.fromEntries(new FormData(form)) return for it?
Show answer
The body is topic=css&topic=html. Object.fromEntries returns { topic: "html" }, because it keeps only the last value for a repeated name. Use new FormData(form).getAll('topic') to get ["css", "html"].