<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[<?php echo $group['id']; ?>]" <?php echo $checked; ?>>
<?php
}
?>
將輸出:PHP獲取複選框價值/未選中
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[1]">
<input type="checkbox" name="delete_user[2]">
<input type="checkbox" name="delete_user[3]">
<input type="checkbox" name="delete_user[4]" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
當我檢查輸入通緝,那麼這將處理輸入。
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
is_checked($del); //@todo
}
}
?>
我在查找是否檢查複選框並返回適當的值(Y或N)。在這一點上,我爲此目的聲明瞭is_checked()函數。
$ _POST ['delete_user']將只包含那些已被選中的項目。 –
那麼,按照HTML表單的工作原理,它只會檢查被檢查的複選框的POST值。 – mituw16