You pre-fill a field the user should not change, such as a plan name or a calculated total, and mark it disabled. The form submits, and your server never receives that field.
The cause
A disabled control is not submitted with the form. MDN's documentation for the disabled attribute says disabled controls "are not submitted with the form". Nothing is wrong with your server or your JavaScript. The browser simply leaves the field out, including when you build a FormData object from the form.
The fix
Use readonly instead of disabled. The value stays visible and the user cannot edit it. This jsdom script builds a FormData from a form with one disabled field, one readonly field, and one normal field, then prints the field names it contains:
<form id="f">
<input name="plan" value="pro" disabled>
<input name="email" value="a@example.com" readonly>
<input name="coupon" value="SAVE10">
</form>const { JSDOM } = require("jsdom");
const dom = new JSDOM(`<form id="f">
<input name="plan" value="pro" disabled>
<input name="email" value="a@example.com" readonly>
<input name="coupon" value="SAVE10">
</form>`);
const { document, FormData } = dom.window;
const data = new FormData(document.getElementById("f"));
console.log([...data.keys()]);
// [ 'email', 'coupon' ]The disabled plan field is gone, while the readonly email field is present.
Caveats
That output comes from jsdom, not a real browser, so confirm the behavior in the browsers you support before relying on it.
Neither attribute is a security control. Anyone can edit either kind of field in dev tools, so recompute values like a plan or a total on the server instead of trusting what the form sends.
Back to All Questions