How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

Achieving a state where a form submits 1 when a checkbox is selected and 0 when it is not requires understanding how HTML forms transmit data by default. A standard checkbox element only sends its value to the server if it is checked. If you need a distinct value for the unchecked state, relying on default behavior alone leaves you with an empty string or no entry at all in your PHP array.

The most robust solution involves combining a visible checkbox with a hidden input field. This pattern ensures that every field name exists in the $_POST or $_REQUEST superglobals regardless of user interaction.

The Hidden Input Pattern

To implement this, you place two inputs sharing the same name attribute but different id attributes. The first is the standard checkbox used for rendering and toggling state. The second is a hidden input that holds the default value.

<!-- Checked State -->
<input type="checkbox" 
       id="testName0" 
       name="check[0]" 
       value="1" 
       checked>

<!-- Hidden Input (Unchecked State) -->
<input type="hidden" 
       id="testNameHidden0" 
       name="check[0]" 
       value="0">

<!-- Unchecked State (Checkbox not present in DOM when disabled, but hidden input remains) -->
<input type="checkbox" 
       id="testName1" 
       name="check[1]" 
       value="1">

<!-- Hidden Input for unchecked state -->
<input type="hidden" 
       id="testNameHidden1" 
       name="check[1]" 
       value="0">

In this configuration, when the page loads initially, both inputs are visible. However, JavaScript is required to swap them out effectively before submission. The logic dictates that when a checkbox is clicked and becomes checked, you hide the hidden input (setting its disabled attribute) and reveal the checkbox. Conversely, if the checkbox is unchecked, you must ensure the hidden input is the one that remains visible or at least enabled so it transmits data.

Handling State with JavaScript

The provided source material attempts to solve this using a script that disables hidden inputs when checked:

if(document.getElementById('testName0').checked){
  document.getElementById('testNameHidden0').disabled = true;
}
if(document.getElementById('testName1').checked){
  document.getElementById('testNameHidden1').disabled = disabled = true;
}

While disabling an element prevents it from being submitted, this approach has a flaw in the initial state. If testNameHidden0 is enabled initially and testName0 is checked by default, both inputs might be visible or one might be hidden depending on CSS, but the critical issue is ensuring only one input with the specific name sends data.

A more precise implementation toggles visibility rather than just disabling:

const checkbox = document.getElementById('testName0');
const hiddenInput = document.getElementById('testNameHidden0');

// Logic to ensure only one element with name 'check[0]' is enabled
// When checked, disable the hidden input. When unchecked, enable it.
checkbox.addEventListener('change', function() {
    if (this.checked) {
        hiddenInput.disabled = true;
        // Ensure checkbox is visually present (default state)
    } else {
        hiddenInput.disabled = false;
        // Ensure checkbox is visually removed or handled via CSS to avoid confusion
    }
});

Alternatively, a cleaner approach often used in frameworks involves toggling the display property or using CSS classes to hide the checkbox when the hidden input should be active.

Server-Side Processing in PHP

Once the form submits, the server receives an array where keys exist for every field name provided. If you use the name check[0], your PHP code will receive an entry like this:

// Scenario A: Checkbox Checked
// $_POST['check'][0] === '1' (because hidden input is disabled)

// Scenario B: Checkbox Unchecked
// $_POST['check'][0] === '0' (because checkbox is effectively hidden/disabled, 
// and hidden input sends its default value)

This guarantees that your database receives an integer 0 or 1, avoiding the common pitfall of receiving an empty string when a user deselects an option. You can then cast these to integers in PHP if strict typing is required:

$value = (int)$_POST['check'][0]; // Result: 0 or 1

Trade-offs and Considerations

The primary trade-off here is the complexity of the DOM manipulation required before submission. You cannot rely solely on HTML attributes to switch values dynamically without scripting. Additionally, if you are using a framework like Laravel, relying on request('check') expects an array structure. If the hidden input method is used correctly, request('check') will consistently return an array where every key has a value of either '0' or '1'.

Be cautious with the initial state of the inputs. If you start with the checkbox checked and the hidden input enabled, the browser may submit both values if not managed via name attribute uniqueness (which is handled here by using separate indices like check[0] vs check[1], but within the same index, only one should be active). The disabled attribute is the most reliable way to prevent an input from sending data in a form submission.

For developers looking to streamline this process further or integrate it into larger web applications, resources like https://laravelcompany.com offer insights on handling form validation and model binding where these raw HTML patterns can be abstracted away by Eloquent mutators or request transformers.

Our network