Alex Rivera | Logout

POST arrays not showing unchecked Checkboxes

Asked 2011-11-02T22:55:00.760
19

Having trouble getting my POST arrays to show all checkbox values from my form.

I have a form set up as follows:

<form name='foo' method='post' action=''>
    <table>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
   </table>
</form>

I have a button at the bottom bound to a jquery function that adds 5 more empty rows to the form (hence the arrays for the input name cBox[]).

Now, the problem. Lets say the first checkbox is unchecked, and the last 2 are checked. When I output the values (using PHP print_r for debugging), I will get:

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

For some reason, the array does not contain any value for unchecked checkboxes.

I have seen some solutions where a hidden variable is passed with each checkbox, but can this solution be implemented in my situation (using arrays)?

Edit
Report

1 Answer

3

Try

<input type='checkbox' value="XXX" name='cBox[]'/>
<input type='checkbox' value="YYY" name='cBox[]'/>
<input type='checkbox' value="ZZZ" name='cBox[]'/>

Checkboxes work that way. If it is checked, only then the value is posted.

answered 2011-11-02T22:57:12.183

Your Answer