Sub topic 5
How names and values become submitted data
This chapter assumes you can write basic HTML, know what a <form> element with action and method does, and have used a text input and a submit button. It explains what the browser puts into the request when a form is submitted.
The submission is a list of name/value pairs
When a form is submitted, the browser builds a list of pairs from the form's controls. Each pair has a name, taken from the control's name attribute, and a value. The server never sees id, labels, or placeholder text. It sees only these pairs.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<button type="submit">Subscribe</button>
</form>If the user types [email protected], the body of the POST request is email=ada%40example.com. The @ is percent-encoded because the default encoding is application/x-www-form-urlencoded.
Which controls are submitted
Only "successful" controls contribute a pair. A control is skipped when any of these is true:
- It has no
nameattribute, or the name is an empty string. - It is
disabled. - It is a checkbox or radio button that is not checked.
- It is a
<select>with no selected option. - It is a button other than the one used to submit the form.
Two consequences come up often:
- A control with
idbut nonameis never sent. This is the most common reason a field "disappears" on the server. - A
disabledfield is not sent, but areadonlyfield is. Usereadonlywhen the user should not edit a value that the server still needs.
Where the value comes from
| Control | Value sent |
|---|---|
| Text, email, and similar inputs | The text the user entered, which can be an empty string |
| `textarea` | The text content, with line breaks normalized |
| Checkbox or radio | The `value` attribute, or `on` if there is none |
| `select` | The `value` of the selected option, or its text if it has no `value` |
| Submit button | Its `value`, only if that button was used to submit |
An empty text field is still sent, as name= with nothing after the equals sign. An unchecked checkbox is not sent at all. Server code therefore has to treat a missing key as "off".
Repeated names
Several controls can share a name. Radio buttons rely on this to form a group, and only the checked one is sent. Checkboxes with the same name each add a pair when checked.
<form action="/signup" method="post">
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<label for="country">Country</label>
<select id="country" name="country">
<option value="de">Germany</option>
<option value="us">United States</option>
</select>
<label>
<input type="checkbox" name="newsletter" value="yes"> Send me the newsletter
</label>
<button type="submit">Create account</button>
</form>With "Pro", "United States", and the checkbox ticked, the body is plan=pro&country=us&newsletter=yes. With the checkbox cleared, the body is plan=pro&country=us.
Reading the pairs in JavaScript
FormData builds the same list of pairs the browser would send. Because names can repeat, Object.fromEntries keeps only the last value for a repeated name. Use getAll when you expect several values.
const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
event.preventDefault();
const data = new FormData(form);
console.log(Object.fromEntries(data));
});Exercise
A form has a checkbox <input type="checkbox" id="terms" value="yes"> and a text field <input name="city" value="">. The user ticks the checkbox and leaves the city empty. What does the request body contain?
Show answer
It contains city=. The checkbox has no name attribute, so it is not sent even though it is checked. The empty text field is still sent with an empty value. Adding name="terms" to the checkbox would add terms=yes.