在PHP中提交后保持选择值

时间:2014-03-13 20:29:36

标签: php

我的php代码与下面类似。如果我选择并提交选项,则所选值将消失。如何在提交后的下拉列表中显示所选值?

     <select name = "org">
      <?php
      $query = "select org from txtable group by org";
      $result = mssql_query ($query);
      while ($record = mssql_fetch_array ($result))
      {
      echo "<option value = '".$record[0]."'>". $record[0]. "</option>";
      }

     ?>
     </select>

注意:我已经搜索了类似的问题,但没有一个真正帮助过我。

1 个答案:

答案 0 :(得分:1)

 <select name = "org">
  <?php
  $query = "select org from txtable group by org";
  $result = mssql_query ($query);
  while ($record = mssql_fetch_array ($result))
  {
    if($_POST['org'] == $record[0])
    {
        echo "<option selected value = '".$record[0]."'>". $record[0]. "</option>";
    }
    else
    {
       echo "<option value = '".$record[0]."'>". $record[0]. "</option>";
    }
  }

 ?>
 </select>

它接受前一个post变量并检查它是否等于在SQL结果中找到的值