2

I've a form with around 80 checkboxes and all of them will be checked by default. The user will only uncheck the unwanted items, so I was looking for unchecked checkbox elements in the POST. However, POST only holds values of checked ones.

After searching a bit, I have found a workaround for my situation: this question

The workaround:

<form>
  <input type='hidden' value='0' name='selfdestruct'>
  <input type='checkbox' value='1' name='selfdestruct'>
</form>

Using this I am able to get values of hidden field and thus my purpose is fulfilled.

But the important question is why is this not a default feature? I have found valid reasons for any technical thing that sounds weird the first time. But for this (cannot post unchecked checkbox) I am still curious to know the reason.

I would appreciate if anyone can shed light on this topic.

Community
  • 1
  • 1
Rakesh Shewale
  • 497
  • 6
  • 22
  • 2
    You are trying to squeeze a square into a circle. This was not intended for the check-box usage, you should use `Radio` types instead. – Orel Eraki Jun 06 '15 at 10:06
  • To change over, maybe I will have to replace each single checkbox with two radio buttons. I am sure Check-box is what I need in the form :) – Rakesh Shewale Jun 06 '15 at 10:12
  • @OrelEraki Checkboxes are just fine for this purpose. You just have to change the way you check them server side. – GolezTrol Jun 06 '15 at 10:33
  • @GolezTrol, Indeed, they are fine, but I would recommended him a different approach if he want to send True or False. – Orel Eraki Jun 06 '15 at 10:53

2 Answers2

5

W3C explains that only controls that are "successful" are submitted.

A successful control is "valid" for submission.

  • All "on" checkboxes may be successful.

Similarly, for radio buttons, only the "on" radio buttons are "successful", for menus, only the selected options are "successful".

Community
  • 1
  • 1
bjfletcher
  • 11,168
  • 4
  • 52
  • 67
  • The link is useful. How about radio buttons when none is selected? Still we can consider them "Valid" for submission? If so, why special treatment for checkbox? – Rakesh Shewale Jun 06 '15 at 10:21
  • 1
    For radio buttons it's the same. If none is selected, the value is not posted. A checkbox, like other controls, has one current value. For inputs you can type that value. For `select`s, you can select that value through an `option`. For a checkbox, the current value is determined by the `value` attribute. A checkbox doesn't have two values (one for checked and one for not checked), but it just has one value which is posted or not, depending on whether the checkbox is checked. The server script should know which checkboxes there were, and any name that is not posted was implicitly not checked. – GolezTrol Jun 06 '15 at 10:28
  • When you have a set of radio buttons where none is checked, this isn't "successful" and nothing about this is submitted at all. – bjfletcher Jun 06 '15 at 10:40
  • I like how you put "valid" between quotes. I really hope this aberration will be fixed in HTML 6... An unchecked checkbox is just as valid as a checked checkbox. Destroying data based on an assumption was a terrible idea. – Guillaume F. Jul 21 '17 at 22:35
1

An unchecked checkbox doesn't get sent in the POST data. You should just check if it is empty:

if (empty($_POST['field1']))
     ....
else
     ....

You have also forgotten to add a correct name and id to your fields. Adding this will make it easier to do checks.

<input type='hidden' value='0' name='field1' id="field1" >
Alex
  • 5,759
  • 1
  • 32
  • 47
  • The input in the question already has a name. id is irrelevant for posting forms. – GolezTrol Jun 06 '15 at 10:31
  • it is not mandatory, but it is useful when applying some javascript. – Alex Jun 06 '15 at 10:40
  • 1
    Of course, but the question is just about posting forms and you claim the id was 'forgotten', which is not true. So the remark about ids adds noise to your answer and actually makes it incorrect. A shame, since the first part of your question is useful. – GolezTrol Jun 06 '15 at 10:44