0

Good Day!

I would like to ask how can I retain the value of my drop down box when I edit it.

in my form

<tr>
<td><strong>*Die Type:</strong></td> 
<td>
        <?php
            $query = "SELECT * FROM idie_type_tbl" ;
            $result = mysql_query($query);
            echo'<select name="die_type"  class="textfield">';
            while($row = mysql_fetch_assoc( $result )) { 
                    echo '<option value="'.$row['die_type_id'].'">' . $row['die_type'] . '</option>';   
            }
            echo '</select>';

        ?>
</td>

same also in my edit.php script. how can I retain the value of my database driven drop down box in edit.php

thanks..

ChocoMartin
  • 111
  • 1
  • 3
  • 12
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jun 14 '13 at 06:39
  • Lol you deleted your that question ...but make sure that you use pdo carefully ..see this answer http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя Jul 30 '13 at 02:20

2 Answers2

1
  1. Get the submitted value from $_GET/POST
  2. While looping over the possible values, test to see if there is a match
  3. Add a selected attribute if there is
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

As @Quentin said follow his instruction and use below code

while($row = mysql_fetch_assoc( $result )) { 
     if($_POST['die_type'] == $row['die_type_id']) {
         echo '<option value="'.$row['die_type_id'].'" selected = "selected">' . $row['die_type'] . '</option>'; 
     } else {
         echo '<option value="'.$row['die_type_id'].'">' . $row['die_type'] . '</option>';
     }  
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100