Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.

Here's my dropdownlist:

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

Here's the 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>";
        }
      }
?>

After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!

Answers


Try this, you need to place select="selected" in the while loop. See below code how I placed the $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>";
?>

Need Your Help

Matlab: sum for two time series dataset?

matlab join matrix time-series

I like to sum up the observations of two time series datasets when both of them have observations based on YEAR and DOY. I also like to skip the sum when either dataset has 0 and only do the sum fo...

moodle: how to change the column size in bootstrap theme 'clean'?

css twitter-bootstrap themes moodle

i'm trying to change the column size of the moodle theme 'clean' which is based on bootstrap.