php 动态生成多选按钮

php 动态生成多选按钮

<?php
function generate_checkboxes($name,$options, $default=array()) {
   if (!is_array($default)){
     $default = array();
   }
   foreach($options as $value => $label) {
      $html .= "<input type=checkbox ";
      if (in_array($value, $default)){
        $html .= "checked ";
     }
     $html .= "name=\"{$name}[]\" value=\"$value\">";
     $html .= $label . "<br>";
   }
   return($html);
}
$options = array("音乐" => "音乐",
                   "电影" => "电影",
                   "互联网" => "互联网",
                   "旅游" => "旅游");
$interests = array("音乐" => "音乐",
                   "互联网" => "互联网"
                   );
$html = generate_checkboxes("interests",$options, $interests);
?>
选择您的爱好:
<form action="" method=post>
<?php echo $html;?>
<input type=submit value="继续">
</form>

<?php

foreach($_REQUEST as $k=>$v){
 echo $k;echo "--";
 echo $v;echo "</br>";
}

echo "</br>";

if(isset($_POST['interests'])){
 foreach($_POST['interests'] as $k=>$v){
  echo $k;echo "--";
  echo $v;echo "</br>";
 }
}

?>

原文地址:https://www.cnblogs.com/hailexuexi/p/2178878.html