0

How to POST checkbox to off for example when it's not checked as checboxes submit values only when it's checked and I don't want to use hidden variables

Thanks

user2285478
  • 33
  • 1
  • 8
  • What's wrong with hidden variables? http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked?rq=1 – DCoder May 26 '13 at 04:10
  • It's sent anyway and I can't trace which checkbox is the checked one for example my checkbox array if no box is checked is 0=>off, 1=>off, 2=>off, 3=>off But if one box is checked it's gonna be 0=>off, 1=>on, 2=>off, 3=>off, 4=>off I am using those indexes to initialize another variable .. if 0 {act=create} elseif (1) {act=delete} and so on .. if the checked box is unknown then I won't be able to initialize my variable correctly .. any ideas? – user2285478 May 26 '13 at 04:15
  • This is clearly work for the server and not for the client. You should not rely on javascript telling your server which checkboxes are not checked (normally the browser wouldn't tell you at tell). Since your server is delivering the form, you should know what the expected response is anyway. – Dave Chen May 26 '13 at 04:23
  • @DaveChen, that's what I am trying to do. I don't know how to know if the box isn't checked without using hidden variables and expanding my checkbox array .. If you have any idea, please let me know. thank you – user2285478 May 26 '13 at 04:27
  • My answer is: Don't use hidden values, you're relying on client input for server action. You need to know what you are expecting serverside to begin with. – Dave Chen May 26 '13 at 04:30

1 Answers1

1

The short answer is the browser doesn't send unchecked checkboxes. But one possible work around is to on the php side set a defaults array

So for a form that included fields like

<input type='checkbox' name='checkbox[a]' value='1'/>
<input type='checkbox' name='checkbox[b]' value='1'/>
<input type='checkbox' name='checkbox[c]' value='1'/>

you would say

$checkbox_defaults = array(
   "a" => 0,
   "b" => 0,
   "c" => 0
);

Then on PHP say

$_POST["checkbox"] = array_merge($checkbox_defaults, $_POST["checkbox"]);

NOTE this only works for string indexed arrays ... if you need to work with numerically indexed arrays the php should look like this.

$checkbox_defaults = array(
   0 => 0,
   1 => 0,
   2 => 0
);
foreach($checkbox_defaults as $k=>$v){
    $_POST["checkbox"][$k] = (isset($_POST["checkbox"][$k])?
                             $_POST["checkbox"][$k]:$v);
}
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • worth noting this technique falls down for non-string key indexed arrays. numerically indexed array you get the defaults **AND** the checked ones. – Orangepill May 26 '13 at 05:06
  • Thank you! But my array is numerically indexed and I don't wanna return both values... I need a way to return only the checked and non checked values .. Any ideas? – user2285478 May 26 '13 at 05:36
  • I would use hidden variables but it does the same.. send the checked and the hidden values .. – user2285478 May 26 '13 at 05:39
  • if the `name` attribute that you are to the checkboxes is like `name='checkbox[]` then the only thing you can really rely on is differing values. if you are putting in the numeric index in the name attribute `name='checkbox[1]` then there is some hope ... I will update the answer for this use case. – Orangepill May 26 '13 at 05:41
  • you are screwed given this markup. Also you can accurately assert is how many boxes where checked for each value of $gid not which ones. I would set something within the `[]` of the checkbox names to differentiate each of the check boxes in the list. – Orangepill May 26 '13 at 06:01