1

当我从数据库表中选择下拉值然后刷新页面时,该值返回到选择选项

这是我的代码:

<select name="customers" id="customers" style="width:150px;"  >
    <option value=""><--Select--></option>
    <?php 
        $query=mysql_query("SELECT cn FROM customerinformation order by id");
        while($row=mysql_fetch_assoc($query))
        {
             $val2=$row['cn'];
    ?>
        <option  value="<?=$val2;?>"
            <?if ($_REQUEST['cn'] == $val2){ echo "selected='selected'"; }?>> 
            <?=$row['cn'];?> 
        </option>
    <?php }?>
</select>

请告诉我或指导我哪里做错了。提前致谢!

4

2 回答 2

1

我不明白你的问题,但我认为你应该把 $_REQUEST['customers'] 而不是 $_REQUEST['cn'] (因为你选择的名字)例子:


<select name="customers" id="customers" style="width:150px;"  >
<option value=""><--Select--></option>
  <?php

  $query=mysql_query("SELECT cn FROM customerinformation order by id");
  while($row=mysql_fetch_assoc($query)) {
      $selected = ($_REQUEST['customers'== $row['cn']?'selected="selected"':'');
      echo '<option value="'.$row['cn'].'" $selected>'.$row['cn'].'</option>';
  }

  ?>
</select>
于 2012-11-12T19:09:25.300 回答
0

您需要 中unset()的值$_REQUEST['cn'],一旦用于评估,

    <? if ($_REQUEST['cn'] == $val2) { 
        echo "selected='selected'"; 
        unset($_REQUEST['cn']); 
    }?> > <?=$row['cn'];?> </option>
于 2012-11-12T18:52:18.323 回答