在多个选择的PHP中保留选定的选项

时间:2011-12-22 04:37:29

标签: php

我需要选择用户在多选菜单中选择的选项。这是我到目前为止的代码,但仍然不起作用。谢谢你的帮助!

<select name="cb_estatura2[]" size="6" multiple="multiple" class="inputbox" id="cb_estatura2">
<?php 
$height = array("57","58","59","60");
$choosen_height = $_GET['cb_estatura2'];

for ($i=0;$i<count($height);$i++) 
     {
      $selected = ($height[$i] == $choosen_height[$i] ? 'selected="selected"' : ''); 
      echo "<option value='$height[$i]' $selected>$height[$i]</option>";
     } 
?>
</select>

1 个答案:

答案 0 :(得分:7)

应该工作的一件事是:

<select name="cb_estatura2[]" size="6" multiple="multiple" class="inputbox" id="cb_estatura2">
<?php 
$height = array("57","58","59","60");
$choosen_height = $_GET['cb_estatura2'];

for ($i=0;$i<count($height);$i++) 
     {
      $selected = (in_array($height[$i],$choosen_height) ? 'selected="selected"' : ''); 
      echo "<option value='$height[$i]' $selected>$height[$i]</option>";
     } 
?>
</select>

这应该可行,只是因为$_GET['cb_estatura2']不像$height数组那样填充。可能是错的,没有测试过。

相关问题