I have:
<select name="topic" style="margin-bottom:3px;">
<option>General Question</option>
<option>Company Information</option>
<option>Customer Issue</option>
<option>Supplier Issue</option>
<option>Request For Quote</option>
<option>Other</option>
</select>
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):
<tr>
<td>Name:*</td>
</tr>
<tr>
<td><input name="name" type="text" size="15" value="<?php echo $name ?>" maxlength="200" /></td>
</tr>
So, any ideas for drop downs?
You need to add the "selected" attribute to the appropriate option. I believe you also need to specify the value attribute for each option. I don't know exactly how you are generating that list, but maybe this will help:
<?php
$options = array( 1=>'General Question', 'Company Information', 'Customer Issue', 'Supplier Issue', 'Supplier Issue', 'Request For Quote', 'Other' );
$topic = $_REQUEST['topic']; // the topic name would now be $options[$topic]
// other PHP etc...
?>
<select name="topic" style="margin-bottom:3px;">
<?php foreach ( $options as $i=>$opt ) : ?>
<option value="<?php echo $i?>" <?php echo $i == $topic ? 'selected' : ''?>><?php echo $opt ?></option>
<?php endforeach; ?>
</select>
First of all, give the option element a value attribute. This makes the code more robust, because it does not break should you decide to alter the text of an option. After that:
<?php $topic = $_REQUEST['topic']; ?>
<?php $attr = 'selected="selected"'; ?>
<select name="topic" style="margin-bottom:3px;">
<option value="1" <?php echo $topic == 1 ? $attr : ''; ?>>General Question</option>
<option value="2" <?php echo $topic == 2 ? $attr : ''; ?>>Company Information</option>
<option value="3" <?php echo $topic == 3 ? $attr : ''; ?>>Customer Issue</option>
<option value="4" <?php echo $topic == 4 ? $attr : ''; ?>>Supplier Issue</option>
<option value="5" <?php echo $topic == 5 ? $attr : ''; ?>>Request For Quote</option>
<option value="6" <?php echo $topic == 6 ? $attr : ''; ?>>Other</option>
</select>