我需要将这段代码更改为一个复选框,将值存储在数组中。怎么办呢?
<div style="float:left; margin-left:20px;">
<select name="category_id">
<?php
foreach($category_lists as $list){
if($list['id'] == $arr_collect['category_id']){
$sel = ' selected="selected" ';
}else{
$sel = null;
}
echo '<option value="'.$list['id'].'"'.$sel.'>'.$list['name'].'</option>';
}
?>
</select>
</div>
答案 0 :(得分:0)
删除<select></select>
代码并尝试:
<?php
foreach($category_lists as $list)
{
if ($list['id'] == $arr_collect['category_id'])
{
$chk = 'checked';
}
else
{
$chk = '';
}
echo '<input type="checkbox" name="list[]" value="' . $list['id'] . '" ' . $chk . '>' . $list['name'];
}
?>
然后您可以像访问它一样访问它:
<?php
$lists = $_POST['list'];
foreach($lists as $list_val)
{
echo $list_val; //prints all the checked values
}
?>