Checkbox status update working only when checkbox is checked in PHP

The checkbox fields are only present in the post array if they are checked. Your php code must account for this.

Let’s assume the name of your checkbox is food1 and the value is 1.
If it is checked, $_POST['food1'] is '1'.
If it is not checked $_POST['food1'] is not defined.

If your php code relies on $_POST['food1'] to exist, you get the problem you describe.

Since you didn’t show your code, this is a educated guess only.

This code is flawed:

foreach ($box as $key => $value)

Like I said, a checkbox that is not checked will not be submitted. Therefore $box will only contain the values of checked checkboxes, but never of those that are not checked.

In order to get your code work, you must get the list of present checkboxes from your database, and then compare to $box to remove unchecked checkboxes.

Leave a Comment