Sub topic 2
Form controls and submission refresher
This chapter assumes you know basic HTML forms, how to select elements and handle events in JavaScript, and that a form submission sends an HTTP request. It reviews the parts of forms that validation builds on.
What a form control is
A form control is an element the user can use to enter or choose data. Examples are input, select, textarea, and button. A form element groups controls and defines where and how their data is sent.
Three attributes matter most for later chapters:
nameis the key under which the control's value is sent. A control with nonameis not submitted.idlinks a control to itslabeland to scripts and CSS.typeselects the kind of input and, later, which built-in checks apply.
Always pair a control with a label using for and a matching id. Clicking the label focuses the control, and screen readers announce the label with it. Error messages in later chapters depend on this link.
What gets submitted
When a form is submitted, the browser builds a list of name and value pairs from the form's successful controls. A control is skipped when any of these is true:
- It has no
name. - It is
disabled. - It is a checkbox or radio button that is not checked.
- It is a button that was not the one used to submit.
Read-only fields (readonly) are submitted. Disabled fields are not. This difference is a common source of "missing" values on the server.
The method attribute chooses how the list is sent. With get, the pairs go in the URL query string, which suits searches. With post, they go in the request body, which suits sign-ups and anything that changes data. The action attribute is the URL that receives the request. If you omit it, the form submits to the current page.
Seeing the data yourself
The FormData object builds the same list in JavaScript. This example prints what a submission would contain without leaving the page:
<form id="demo" action="/signup" method="post">
<label for="name">Name</label>
<input id="name" name="name" value="Ada">
<label for="note">Note</label>
<input id="note" value="No name attribute">
<label for="locked">Locked</label>
<input id="locked" name="locked" value="disabled" disabled>
<label><input type="checkbox" name="news" value="yes"> Newsletter</label>
<button type="submit">Send</button>
</form>
<script>
const form = document.getElementById('demo');
form.addEventListener('submit', (event) => {
event.preventDefault();
const data = new FormData(form);
console.log(Object.fromEntries(data));
});
</script>The console shows { name: "Ada" }. The note field has no name, locked is disabled, and the checkbox is unchecked, so none of them appear. If you check the box, news: "yes" is added.
Where validation fits
Validation runs at one specific point in the submission process. Here is the sequence.
Two points follow from this diagram. First, the submit event does not fire when built-in validation fails, so your submit handler only runs for forms that already passed. Second, calling preventDefault() in that handler stops the request, which is how single-page apps send data with fetch instead.
Common errors
- A
buttoninside a form defaults totype="submit". A button meant for something else, such as "show password", needstype="button", or it will submit the form. - Radio buttons in one group must share the same
name. Otherwise the user can select several, and the server cannot tell which question each answers. - Reading
input.valuein a script returns a string, even fortype="number". Convert it yourself. - Using
disabledto stop a user editing a value also drops it from the submission. Usereadonlyif the server still needs the value.
Exercise
A form has a text input with id="city" and a label, but the server never receives a city value. The input is not disabled. What is the most likely cause, and how do you fix it?
Show answer
The input has no name attribute, so the browser leaves it out of the entry list. Add name="city" to the input.