Disabled form fields are not POSTed
By Flavio
Published · Updated
Tagged: HTML
A quick gotcha: disabled form fields are not sent in a POST request, so use the readonly attribute instead when you want a field that still gets submitted.
Was working with a form where I wanted a field to not be edited by users, so I used the disabled attribute on it.
But, it wasn’t sent to the server when the form was submitted.
I eventually used the readonly attribute to make it uneditable, yet be sent in the POST request.
What gets sent and what doesn’t
Take this form:
<form method="post" action="/submit">
<input name="plan" value="pro" disabled>
<input name="email" value="flavio@example.com" readonly>
<button>Save</button>
</form>
When you submit it, the request body is:
email=flavio%40example.com
plan is gone. The browser skips disabled controls when it collects the data to send. It’s not a bug, it’s how HTML forms work.
new FormData(form) in JavaScript collects the data the same way, so a disabled field is missing there too:
const form = document.querySelector('form')
console.log([...new FormData(form)])
// [["email", "flavio@example.com"]]
disabled vs readonly
Both stop the user from typing in the field. The differences:
- a
readonlyfield is sent with the form, adisabledfield is not - a
readonlyfield can get focus, so the user can tab to it and select and copy the text. A disabled field is skipped when tabbing - browsers show disabled fields greyed out. A readonly field looks like a normal field unless you style it
You can style them with the :disabled and :read-only pseudo-classes:
input:read-only {
background-color: #eee;
}
readonly only works on text-like fields
This is the next gotcha. readonly works on <textarea> and on <input> with these types: text, search, tel, url, email, password, date, month, week, time, datetime-local and number.
It’s ignored on <select>, checkboxes, radio buttons, and range, color, file and hidden inputs. A <select readonly> can still be changed by the user.
For those, a common fix is to disable the visible control and add a hidden input with the same name that carries the value:
<select name="country" disabled>
<option value="IT" selected>Italy</option>
<option value="FR">France</option>
</select>
<input type="hidden" name="country" value="IT">
The disabled select shows the value and is not sent. The hidden input is sent.
Re-enable fields right before submit
Another option is to keep disabled for the look and remove it when the form is submitted:
const form = document.querySelector('form')
form.addEventListener('submit', () => {
form.querySelectorAll('[disabled]').forEach((el) => {
el.disabled = false
})
})
The submit event fires before the browser collects the form data, so the fields are enabled again in time and they get sent.
A disabled fieldset disables everything inside it
If you put disabled on a <fieldset>, every control inside it becomes disabled too, and none of them are sent:
<fieldset disabled>
<legend>Billing</legend>
<input name="vat" value="IT123">
</fieldset>
vat is not in the POST body. The only exception is a control placed inside the fieldset’s first <legend>, which stays enabled.
Other fields that are not sent
While you’re debugging a missing value, check these too:
- no
nameattribute: a field without anameis never sent, even if it has a value - unchecked checkboxes and radios: they send nothing at all, not
false. On the server, a missing key means unchecked - buttons: only the button that submitted the form sends its
nameandvalue
required is ignored on readonly fields
A readonly field doesn’t take part in form validation. If you add required to it, the browser ignores it and submits the form even when the field is empty.
Don’t trust readonly values on the server
readonly and disabled only stop people from editing the field in the page. Anyone can open DevTools, remove the attribute and change the value, or send a POST request by hand.
If a value must not change, like a price or a user ID, don’t take it from the form. Read it from your database or the session on the server, and validate everything else the form sends.
The FormData object post shows how to read and send form data with JavaScript.
Want me to talk about your product? You can sponsor this site.