How can I keep the selected option from a drop down to stay selected on submit?

I have the following code:

General Question Company Information Customer Issue Supplier Issue Request For Quote Other

for the drop down. And when the form is submitted, It goes to a validation page. If it has errors the form keeps the original content the user put in. I have it working for all of the input fields and textarea’s, but how could I do this with a drop down?

I have the input fields staying by using:

$name = $_REQUEST[“name”];
and in the form that shows up again, there is (ignore the fact that it is in a table):

Name:*

So, any ideas for drop downs?

The only way I’ve done it in the past is using an if statement when the page compiles the HTML code and if the request information matches the option that is getting created, it defaults to that option. Here’s an example based off of your code that you provided:

[php]
$options = array(‘general’ => ‘General Question’, ‘info’ => ‘Company Information’, ‘issue’ => ‘Company Issue’, ‘supissue’ => ‘Supplier Issue’, ‘quote’ => ‘Request For Quote’, ‘other’ => ‘Other’);

$topic = $_REQUEST[‘topic’];

echo ‘’;

foreach($options as $key => $value)
{
echo ‘<option name="’.$key.’"’;
if($topic == $key) echo ’ selected=“selected”’;
echo ‘>’.$value.’’;
}

echo ’

[/php]

This is how I usually do it and other people might have an easier way, but I’ve done it this way and it’s worked all this time. Also you might want to add \n (newline) and \t (tab) in that code so it doesn’t appear to be one long line of code. Hope this helps!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service