5

I have a form with many checkboxes.

ex.

...
<input name="dodatkowe[]"  type="checkbox" value="1" />
<input name="dodatkowe[]"  type="checkbox" value="1" />
<input name="dodatkowe[]"  type="checkbox" value="1" />
...

I want to have all the checkboxes in the array. Array 'dodatkowe'.

When i checked all checkboxes have:

Array ( [0] => 1 [1] => 1 [2] => 1 ) 

but when i checked example only second I have:

Array ( [0] => 1 ) 

I need that, when i check example second checkbox:

Array ( [0] => 0 [1] => 1 [2] => 0)
1
  • 2
    Browsers only submit checked checkboxes, so you will never see values for unchecked checkboxes. Commented Jan 11, 2012 at 21:27

2 Answers 2

4

give them indexes so you can reference them specifically...

...
<input name="dodatkowe[1]"  type="checkbox" value="1" />
<input name="dodatkowe[2]"  type="checkbox" value="1" />
<input name="dodatkowe[3]"  type="checkbox" value="1" />
...

Not sure why you feel you need to see the unchecked values, this can be assumed to be the inverse of the checked values.... Any attempt to do this is a hack, and is unnecessary.

Sign up to request clarification or add additional context in comments.

1 Comment

Possibly, each preceded by a: <input name="dodatkowe[1]" type="hidden" value=0"> (alter the number after dodatkowe[ accordingly of course).
2

If a checkbox isn't checked it won't include it's value into the parameters but the first step would be to give the checkboxes a unique id:

<input name="dodatkowe[0]"  type="checkbox" value="1" />
<input name="dodatkowe[1]"  type="checkbox" value="1" />
<input name="dodatkowe[2]"  type="checkbox" value="1" />

Then you can use PHP to check is the value is there:

$maxfields = 3;
$selectboxes = $_REQUEST['dodatkowe'];
for($i = 0; $i < $maxfields; $i++)
  if(!isset($selectboxes[$i])) $selectboxes[$i] = 0;

This will set all non existent fields to 0 and $selectboxes should contain the result you are looking for.

2 Comments

Not sure I like (or would reccomend) directly modifying the $_REQUEST... might get confusing, depending on what further processing you need to do with submitted values.
True... you might want to consider to copy that over to another variable before...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.