php让checkbox复选框初始选中,很常用的功能,备忘下:
代码如下:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
$arr=array(0=>array('id'=>100,'title'=>'京东'),1=>array('id'=>200,'title'=>'天猫'),2=>array('id'=>300,'title'=>'苏宁'));
$arrId=array(100,300);
if( isset($_POST['mall']) ){
echo '<pre />';
print_r($_POST['mall']);
exit();
}
?>
<form action="" method="post">
<?php foreach ($arr as $key=>$value){?>
<?php if( in_array($value['id'],$arrId) ){?>
<input type="checkbox" name="mall[]" value="<?php echo $value['id']?>" checked><?php echo $value['title']?>
<?php }else{?>
<input type="checkbox" name="mall[]" value="<?php echo $value['id']?>"><?php echo $value['title']?>
<?php }?>
<?php }?>
<input type="submit" value="提交" />
</form>