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

I have been trying to work out, why my php code will not retain the drop down box selection after the submit has been pressed, but haven’t been able to work it out. Could someone please tell me how I can get this to work? Thank in advance.


<select name="nature">							
<option value="none1">Please Select</option>
    <?php if (isset($_POST['submitbtn']) == true) { ?>
             <?php $getstarted1 = "<option value=\\"getstarted\\" selected=\\"selected\\">Get Started</option>";
             echo $getstarted1; ?>
      <?php } else { ?>
             <?php $getstarted2 = "<option value=\\"getstarted\\">Get Started</option>";
              echo $getstarted2; ?>
      <?php } ?>

<?php // drop down box
if (isset($_POST['submitbtn']) == true) { ?>
        <?php $quote1 = "<option value=\\"quote\\" selected=\\"selected\\">Quote</option>";
        echo $quote1; ?>
<?php } else { ?>
         <?php $quote2 = "<option value=\\"quote\\">Quote</option>";
	echo $quote2; ?>
<?php } ?>
</select>

You’re testing if $_POST[‘submitbtn’] isset to decide whether to select each of the <options>.
So if the form has been submitted, then both <options> are going to have selected attributes set.

Try this:


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