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


Keep selected options in multiple select 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>

应该是这样的:

<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数组那样填充。可能是错的,还没测试过