I have ran into this situation before and I resolved by placing a hidden input before the checkbox
with the same name. If the checkbox
is checked then that value will override the hidden. This should work for you.
The second input
always overrides the first. In this case checkboxes
don't POST
if unchecked which means the hidden input would POST
a value of 0
PHP:
<?php
if (isset($_POST['ch1'])) {
echo '<pre>', print_r($_POST['ch1'], true), '</pre>';
echo '<pre>', print_r($_POST['ch2'], true), '</pre>';
echo '<pre>', print_r($_POST['ch3'], true), '</pre>';
echo '<pre>', print_r($_POST['ch4'], true), '</pre>';
}
?>
HTML:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<!-- Row 1 Checkboxes -->
<input type="hidden" value="0" name="ch1[0]" />
<input type="checkbox" value="1" name="ch1[0]" />
<input type="hidden" value="0" name="ch2[0]" />
<input type="checkbox" value="1" name="ch2[0]" />
<input type="hidden" value="0" name="ch3[0]" />
<input type="checkbox" value="1" name="ch3[0]" />
<input type="hidden" value="0" name="ch4[0]" />
<input type="checkbox" value="1" name="ch4[0]" />
<br />
<!-- Row 2 Checkboxes -->
<input type="hidden" value="0" name="ch1[1]" />
<input type="checkbox" value="1" name="ch1[1]" />
<input type="hidden" value="0" name="ch2[1]" />
<input type="checkbox" value="1" name="ch2[1]" />
<input type="hidden" value="0" name="ch3[1]" />
<input type="checkbox" value="1" name="ch3[1]" />
<input type="hidden" value="0" name="ch4[1]" />
<input type="checkbox" value="1" name="ch4[1]" />
<!-- And so forth... -->
<input type="submit">
</form>
[x] [ ] [x] [ ]
[ ] [x] [ ] [x] [ SUBMIT ]
Output:
Array
(
[0] => 1
[1] => 0
)
Array
(
[0] => 0
[1] => 1
)
Array
(
[0] => 1
[1] => 0
)
Array
(
[0] => 0
[1] => 1
)
Edit
$i = 0;
while($recomObject = mysql_fetch_object($recomResult)){
echo '
<tr>
<td>'.$recomObject->op.'</td>
<input type="hidden" value="0" name="ch1['.$i.']" />
<input type="hidden" value="0" name="ch2['.$i.']" />
<input type="hidden" value="0" name="ch3['.$i.']" />
<input type="hidden" value="0" name="ch4['.$i.']" />
<td align="center"><input type="checkbox" value="1" name="ch1['.$i.']" /></td>
<td align="center"><input type="checkbox" value="1" name="ch2['.$i.']" /></td>
<td align="center"><input type="checkbox" value="1" name="ch3['.$i.']" /></td>
<td align="center"><input type="checkbox" value="1" name="ch4['.$i.']" /></td>
<td><input type="hidden" name="recomID[]" value="'.$recomObject->id.'"/>
</td>
</tr>';
$i++;
}