Gumbo is right.然而,有一项工作,其中包括:
换句话说:具有与复选框同名的隐藏字段和表示未选中状态的值,例如0.但是,将隐藏字段放在表单中的复选框之前是很重要的.否则,如果选中该复选框,则隐藏字段的值将在发布到后端时覆盖复选框值.
另一种跟踪这种情况的方法是在后端有一个可能的复选框列表(例如,甚至在后端填充该列表中的表单).像下面这样的东西应该给你一个想法:
$checkboxes = array(
array( 'label' => 'checkbox 1 label', 'unchecked' => '0', 'checked' => '1' ),
array( 'label' => 'checkbox 2 label', 'unchecked' => '0', 'checked' => '1' ),
array( 'label' => 'checkbox 3 label', 'unchecked' => '0', 'checked' => '1' )
);
if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) == 'post' )
{
foreach( $checkboxes as $key => $checkbox )
{
if( isset( $_POST[ 'checkbox' ][ $key ] ) && $_POST[ 'checkbox' ][ $key ] == $checkbox[ 'checked' ] )
{
echo $checkbox[ 'label' ] . ' is checked, so we use value: ' . $checkbox[ 'checked' ] . '
';
}
else
{
echo $checkbox[ 'label' ] . ' is not checked, so we use value: ' . $checkbox[ 'unchecked' ] . '
';
}
}
}
?>
<?php foreach( $checkboxes as $key => $checkbox ): ?>
…选中一个或两个复选框,然后单击“提交”按钮,看看会发生什么.