1

Sorry in advance for the novice question here...

I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.

However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?

<select name="test_select" style="width: 110px">
    <option disabled="disabled" selected="selected">
    <?php 
        if(!empty($_POST['test_select'])){
            echo $_POST[test_select'];} 
        else 
            echo "Select Option"; ?>
    </option>
    <?php $sql = mysql_query("SELECT test FROM test_settings"); 
    while ($row = mysql_fetch_array($sql)){
        ?><option><?php echo $row['test']; ?></option><?php }?>
</select>

Thanks in advance,

Dan

runDOSrun
  • 10,359
  • 7
  • 47
  • 57
Daniel-Barnes
  • 57
  • 1
  • 6

1 Answers1

1

I suppose that problem is that forms are not sending disabled values.

I would edit code as following:

<select name="test_select" style="width: 110px">
<?php 
    if (empty($_POST['test_select']))
      echo '<option selected="selected">Select Option</option>'; 
    $sql = mysql_query("SELECT test FROM test_settings"); 
    while ($row = mysql_fetch_array($sql)){
      $selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select'] 
        ? ' selected="selected"' 
        : '';
      echo '<option'.$selected.'>'.$row['test'].'</option>'; 
?>
</select>
TCFDS
  • 584
  • 4
  • 16
  • This is true, without disabling it does work. However after a second thought, I will need to maintain the post value for further use, which is another problem on it's own; I will probably have to use sessions. Thanks. – Daniel-Barnes Feb 21 '15 at 15:34