2011-04-10 87 views
0

就像標題所說,我在實現它時遇到困難。按鈕點擊後保持最後從下拉列表中選擇的值

這裏是我的下拉列表:

<?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>"; 
?> 

這裏的buttonclick:

<?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數據庫的日期的第一個值,而不是我選擇的那個。如果有人知道如何將選定的值保存在列表中,那麼任何幫助都會非常有用!

回答

0

試試這個,你需要將select="selected"放在while循環中。請參見下面的代碼,我怎麼放在$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>"; 
?> 
+0

嗯,這似乎是合乎邏輯的,但所選擇的選項仍然不存在。現在我真的很困惑.. – Skittl3s 2011-04-10 14:55:26

相關問題