Post Arrays Not Showing Unchecked Checkboxes

POST arrays not showing unchecked Checkboxes

That behavior is not surprising, as the browser doesn't submit any value for checkboxes that are unchecked.

If you are in a situation where you need to submit an exact number of elements as an array, why don't you do the same thing you do when there's an id of some sort associated with each checkbox? Just include the PHP array key name as part of the <input> element's name:

  <tr>
<!-- NOTE [0] --->
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[0]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[1]'/></td>
</tr>
<tr>
<td class='bla'>Checkbox: <input type='checkbox' name='cBox[2]'/></td>
</tr>

That still leaves you with the problem that unchecked boxes will still not be present in the array. That may or may not be a problem. For one, you may really not care:

foreach($incoming as $key => $value) {
// if the first $key is 1, do you care that you will never see 0?
}

Even if you do care, you can easily correct the problem. Two straightforward approaches here. One, just do the hidden input element trick:

  <tr>
<td class='bla'>
<input type="hidden" name="cBox[0]" value="" />
Checkbox: <input type='checkbox' name='cBox[0]'/>
</td>
</tr>
<tr>
<td class='bla'>
<input type="hidden" name="cBox[1]" value="" />
Checkbox: <input type='checkbox' name='cBox[1]'/>
</td>
</tr>

And two, which I find preferable, fill in the blanks from PHP instead:

// assume this is what comes in:
$input = array(
'1' => 'foo',
'3' => 'bar',
);

// set defaults: array with keys 0-4 all set to empty string
$defaults = array_fill(0, 5, '');

$input = $input + $defaults;
print_r($input);

// If you also want order, sort:

ksort($input);
print_r($input);

See it in action.

Unchecked Check-boxes are not fetched in array variable

You can assign indices to the check and provide the value to each checkbox:

<form action="test.php" method="POST">
<input type="checkbox" name="check[0]" value="1">
<input type="checkbox" name="check[1]" value="1">
<input type="checkbox" name="check[2]" value="1">
<input type="checkbox" name="check[3]" value="1">
<input type="checkbox" name="check[4]" value="1">
<input type="checkbox" name="check[5]" value="1">
<input type="submit">
</form>

So, in test.php:

$arr = str_split("000000");
echo join(array_replace($arr, $_POST['check']));

Explanation:

  • str_split - Just to initialize an empty array with zero values
  • array_replace - Replace 0s with the values you supplied in HTML checkbox
  • join - Join the array to a string

Update: Screenshot

Sample Image

POST unchecked HTML checkboxes

Add a hidden input for the checkbox with a different ID:

<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden' type='hidden' value='No' name='testName'>

Before submitting the form, disable the hidden input based on the checked condition:

form.addEventListener('submit', () => {
if(document.getElementById("testName").checked) {
document.getElementById('testNameHidden').disabled = true;
}
}

Unchecked checkboxes in an array

Actually, there is a decent workaround for this as proposed by Sam in this answer on Stack Overflow:

Post the checkboxes that are unchecked

It worked for me, and I suspect you and I had a similar problem (mine being that I had/have upwards of 300 input fields in similar(ish) groups and didn't want to write validation rules for every one of those individual fields, just rules targetted at each family of input types e.g. the email addresses, or the postcodes. In brief, the technique is that you place a hidden input field, with the same name, before your checkbox field. Setting the value of the hidden field (type='hidden') to '0' will ensure that at least one key/value appears in your POST array, with the '0' being superceded by a later '1' only if the box is checked. I needed the '0' value to allow people to 'unset' an option they had previously 'set', for example that they were willing to show their contact data. This technique allows me to present the user with much the same form for an update as they would get at at first registration. Thanks to Sam!

Nette getHttpData include of unchecked checkboxes

I eventually made it with specific key in input name name='golyPenalta[key]' when I create dynamic numbers of this field. It can be done either way by Nette form name='golyPenalta[{key}]' or I create it by JavaScript in for loop when I get the number of fields I have to create like this name='golyPenalta[$i]'.

In form submit I use $form->getHttpData($form::DATA_TEXT | $form::DATA_KEYS, "golyPenalta[]") to persist these keys.

How to deal with unchecked checkbox array value without javascripts?

If you name the checkboxes with an index in them, like so:

<input type="checkbox" name="chk_<?php echo $i ?>" value="1">

Then you could loop through them like so:

<?php
$chkBoxes = array();
foreach ($_POST as $k => $v) {
if (strpos("chk_",$k) === 0) {
$cbIndex = str_replace('chk_', '', $k);
$chkBoxes[$cbIndex] = $v;
}
}

Then to test if a checkbox was checked and sent to the server, you could use:

<?php 
if (isset($chkBoxes[$cbIndex]))

Remember - the value of the checkbox is only sent if it was checked: Does <input type="checkbox" /> only post data if it's checked?



Related Topics



Leave a reply



Submit