我以前遇到过这种情况,我通过checkbox
在同名之前放置一个隐藏的输入来解决。如果checkbox
选中,则该值将覆盖隐藏。这应该适合你。
第二个input
总是覆盖第一个。在这种情况下,如果未选中checkboxes
则不要POST
,这意味着隐藏输入POST
的值为 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 ]
输出:
Array
(
[0] => 1
[1] => 0
)
Array
(
[0] => 0
[1] => 1
)
Array
(
[0] => 1
[1] => 0
)
Array
(
[0] => 0
[1] => 1
)
编辑
$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++;
}