← Back to HTML & CSS, page 6

html-css-0108 · Hard senior

A field is missing from the data the server receives. Which causes would you check in the form?

A control is sent only with a name, when it is not disabled, when it belongs to the form, and, for checkboxes and radios, when it is checked. Print new FormData(form) to see what will be sent. Use readonly when the value must be sent but not edited.

Full answer

When a field is missing, the cause is almost always in the markup. The browser follows fixed rules about which controls are sent.

A control is sent only if all of these are true:

  • It has a name attribute. An id alone is not enough.
  • It is not disabled. A disabled control is skipped. A readonly control is still sent, so use readonly when the user must not edit a value but the server needs it.
  • It belongs to the form. It must be inside the form, or point to it with the form attribute.
  • For checkboxes and radio buttons, it must be checked. An unchecked checkbox sends nothing, not even false. The server must treat a missing key as off.
  • For a select, an option must be selected. For a button, only the button used to submit is sent, with its name and value.
  • For file inputs, the form needs the multipart encoding to send the file itself.

Other causes: two controls with the same name make a list of values and not one value, a control created after submit is too late, and a fieldset disabled disables every control inside it.

A fast way to see what will be sent is new FormData(form). Print its entries in the console before blaming the server.

Reading Object.fromEntries(new FormData(f)) gives a, d, g and i. The field with only an id, the disabled field, the unchecked box and the field inside the disabled fieldset are missing. The field outside the form is present because it uses the form attribute.

When the data is fine in the browser, look at the encoding of the request and at the server code.

html
<form id="f">
  <input name="a" value="1">
  <input id="b" value="2">
  <input name="c" value="3" disabled>
  <input name="d" value="4" readonly>
  <input type="checkbox" name="e" value="on">
  <input type="checkbox" name="g" value="on" checked>
  <fieldset disabled><input name="h" value="8"></fieldset>
</form>
<input name="i" value="9" form="f">

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • A control without a name is not sent.
  • Disabled controls are skipped; readonly ones are sent.
  • Unchecked checkboxes and radios send nothing.
  • The form attribute can attach a control from outside.
  • Inspect FormData before blaming the server.

Common mistakes

  • Giving a field an id but no name.
  • Using disabled for a value that the server still needs. Use readonly, or a hidden input.
  • Expecting an unchecked checkbox to send false.

Follow-ups

  • What is the difference between readonly and disabled?
  • How would you send a value for an unchecked checkbox?

Tips

  • List the rules as a checklist: name, not disabled, in the form, checked.
  • Show FormData as the debug tool, so you test before guessing.