按钮单击后,从下拉列表中保留最后选择的值

时间:2011-04-10 14:27:24

标签: php drop-down-menu

就像标题所说我在实现它时遇到了困难。

这是我的下拉列表:

<?php
      $query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
      $result = mysql_query ($query);
      echo "<select name=data value=''>Data</option>";
      while($nt=mysql_fetch_array($result)){
        echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
      }
      echo "</select>";
?>

这是按钮点击:

<?php
      if(isset($_POST['Submit']))
      {
        $query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]'  AND data ='".$_POST['data']."' group by paskirtis";
        $result = mysql_query ($query);
        echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
        while($nt=mysql_fetch_array($result)){
            if($nt['SUM(suma)'] != null){
            $suma = $nt['SUM(suma)'];
        }
            echo "<tr><td>$nt[paskirtis]</td>
            <td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
        }
      }
?>

按下它之后,它从我从下拉列表中选择的日期检索我想要的数据,并重置整个下拉列表,显示sql数据库中日期的第一个值,而不是我选择的那个。如果有人知道如何将所选值保留在列表中,那么任何帮助都会受到极大的关注!

1 个答案:

答案 0 :(得分:0)

试试这个,您需要在while循环中放置select="selected"。请参阅下面的代码我如何放置$selected

<?php
      $query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
      $result = mysql_query ($query);
      echo "<select name=data value=''>Data</option>";
      while($nt=mysql_fetch_array($result)){
        $selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
            echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
      }
      echo "</select>";
?>