tags:

views:

21

answers:

1

As the title says, How would I do this?

So lets say if the user left message empty but selected a correct topic, I want that topic to be selected after the page has refreshed

My code right now:

if (count($_POST) > 0) {

    if (strlen($_POST['topic']) < 1) 
        $errorMsg = "Please select a topic.";
else if (strlen($_POST['message']) < 1)
    $errorMsg = "You have to enter a message.";

}


$topics = mysql_query("SELECT name FROM topics ORDER BY name ASC");

echo '<option value="-1" selected="selected">(Choose Topic)</option>';
while ($t = mysql_fetch_assoc($topics)) {
    echo '<option value="'.$t['id'].'">'.htmlspecialchars($t['name']).'</option>';
}
A: 

You already know that selected="selected" will make that option selected, so you're almost there.

Think about this, inside your loop:

if ($_POST['topic'] == $t['id']) 
    $selected = ' selected="selected';
else 
    $selected = '';
echo '<option value="'.$t['id'].'"' . $selected.'>'.htmlspecialchars($t['name']).'</option>';
timdev