My drop down box code will not retain the selection from Mysql after submitting?

<?php
$nature = null;
if(isset($_POST['nature'])) {
   $nature = $_POST['nature'];
}
?>
<select name='nature'>
  <option value='none1'>Please Select</option>
  <?php
   $selected = ($nature == 'getstarted') ? 'selected' : '';
   echo "<option value='getstarted' $selected>Get Started</option>";
   
   $selected = ($nature == 'quote') ? 'selected' : '';
   echo "<option value='quote' $selected>Quote</option>";
   ?>
</select>

This code i downloaded from this site. It’s working perfectly. But i need dropdown should display data from Mysql and it should retain the selected value after submitting in php.
Thank in Advance…

Can we assume this code is within form tags with method=“post”, a submit input and being run in a php environment? You are setting $nature with POST and so to convert this it using value from the DB you will need to UPDATE or INSERT post data to your DB table then query for this record and set the variable with the DB result.

Here is my code

  <form  method="post" name="addOrder" action ="" onsubmit="return validateOrder();">
  <?php
$query = mysql_query("SELECT * FROM table");

echo '<select name="order" >'; 
echo '<option value="">SELECT</option>' ;

while ($row = mysql_fetch_array($query)) {

   echo '<option value="'.$row['order_id'].'">'.$row['order'].'</option>';
}

echo '</select>';
 ?>

  <input  type="text" name="orderName" maxlength="80" size="30" id="txt_sub_fmy">
  <input type="submit" name="submit" id="submit" value="Submit" />

</form>

Here If I select dropdown value then i click submit validateOrder() is working correctly. But the problem is dropdown value not retain selected value, it showing “SELECT”. I need to retain the selected values…

I need to submit my data like

Category1
 ->Order1
 ->Order2
 ->Order3

Category1
 ->Order4
 ->Order5
 ->Order6

Category2
 ->Order1
 ->Order2
 ->Order3

Like this it going on. Is my code or wrong?

Please do reply me…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.