HTML Checkbox Unchecked Value: Send a Default With a Hidden Input

Tested
What changed
Rewritten and tested in Chrome 156, PHP 8.5, Express 5.3, Flask 3.1, Django 4.2 and Rack 3.2.
First written
2014
On this page: 7 sections

An unchecked checkbox sends nothing when its form is submitted. The field is not sent as 0, false or an empty string: it is left out of the request entirely, so the server can’t tell “unchecked” apart from “this field was never on the form”. The usual fix is a hidden input with the same name, placed right before the checkbox, that carries the default value.

The hidden input trick

<input type="hidden" name="newsletter" value="0">
<input type="checkbox" name="newsletter" value="1">Code language: HTML, XML (xml)

Browsers send form fields in the order they appear in the page. Submitting this form in Chrome sent these request bodies:

  • Checkbox unchecked: newsletter=0
  • Checkbox checked: newsletter=0&newsletter=1

When the box is checked, the default is still sent, followed by the checkbox’s value. The trick depends on the server keeping the last value of a repeated name. That is true for PHP, but not for every backend (see the table below). The hidden input has to come before the checkbox: put it after, and a last-value server always reads 0.

Always give the checkbox a value

A checked checkbox without a value attribute sends the string on. That works, but newsletter=on next to a hidden 0 makes for messy server code. Set value="1" (or yes, or whatever your database expects) so both states use the same kind of value.

How your backend reads the repeated name

I posted newsletter=0&newsletter=1 (a checked box) to each of these and read the field the obvious way:

BackendCodeChecked box reads asTrick works?
PHP 8.5$_POST['newsletter']"1"Yes
Ruby on Rails (Rack 3.2)params[:newsletter]"1"Yes
Django 4.2request.POST['newsletter']"1"Yes
Flask 3.1 (Werkzeug)request.form['newsletter']"0"No, first value wins
Express 5.3req.body.newsletter with express.urlencoded()["0", "1"]No, you get an array
Standard FormData / URLSearchParamsformData.get('newsletter')"0"No, first value wins

Rails is the reason the trick is so widespread: its checkbox form helper outputs exactly this hidden field before every checkbox. The last row matters more than it looks. Anything that hands you a standard FormData object reads the first value, and that includes Next.js server actions, Remix actions, Cloudflare Workers, Deno and Bun. With the hidden input in place, formData.get() returns "0" whether the box is checked or not.

On those backends, read the last value explicitly:

# Flask
newsletter = request.form.getlist('newsletter')[-1]

// Express
const newsletter = [].concat(req.body.newsletter).at(-1);

// FormData (Next.js, Remix, Workers, Deno, Bun)
const newsletter = formData.getAll('newsletter').at(-1);Code language: plaintext (plaintext)

Or skip the hidden input

If your code knows which checkboxes the form contains, you don’t need the hidden field at all. Treat a missing field as unchecked:

$newsletter = isset( $_POST['newsletter'] ) ? 1 : 0;Code language: PHP (php)
const newsletter = formData.has('newsletter');Code language: JavaScript (javascript)

This is usually the cleaner option on Flask, Express and FormData backends. The hidden input earns its keep when the server saves whatever fields arrive without knowing the form, like a generic “update this record from the request” handler. Without the default, unchecking a box there changes nothing, because nothing is sent.

Disabled checkboxes send only the default

Disabled fields are never submitted. Disable a checked checkbox and leave its hidden input alone, and the form sends newsletter=0: saving the form quietly switches the setting off. Disable the hidden input together with the checkbox, so neither is sent and the server keeps the stored value:

function setNewsletterLocked(locked) {
  document.querySelectorAll('[name="newsletter"]').forEach((el) => {
    el.disabled = locked;
  });
}Code language: JavaScript (javascript)

(A readonly attribute doesn’t help here: it has no effect on checkboxes.)

Checkbox groups

For a group of checkboxes sharing a name such as colors[], don’t put a hidden input before each one: every hidden 0 would end up in the array. Add a single hidden input with an empty value before the group instead:

<input type="hidden" name="colors[]" value="">
<label><input type="checkbox" name="colors[]" value="red"> Red</label>
<label><input type="checkbox" name="colors[]" value="blue"> Blue</label>Code language: HTML, XML (xml)

PHP then receives [""] when nothing is checked and ["", "red"] when only Red is. The group is always present, and you drop the empty string before saving:

$colors = array_values( array_filter( $_POST['colors'] ?? [], 'strlen' ) );Code language: PHP (php)

Sending checkbox state with JavaScript

If you submit with fetch() and JSON instead of a regular form post, skip all of this and send the checkbox’s checked property, which is a real boolean:

const body = JSON.stringify({
  newsletter: document.querySelector('#newsletter').checked,
});Code language: JavaScript (javascript)

Select the checkbox by ID or type there, not as form.elements.newsletter. With a hidden input of the same name, that returns a list of both inputs, and the list has no checked property.

More re-tested in Other

3 comments

Show 3 comments left before the 2026-10-27 re-test

These were written about the earlier version of this post, so the code and steps they discuss may no longer be on the page.

  1. Sorry, but by using this code it results in [ ‘0’, ‘1’ ] value when the checkbox is checked .. Is there a way that i missunderstood what you did ?

Leave a comment

Your email address won’t be published. Name, email and comment are required.