Php 记得选择菜单选项吗

Php 记得选择菜单选项吗,php,Php,正如标题所说,我将如何做到这一点 因此,假设用户留下的消息为空,但选择了正确的主题,我希望在页面刷新后选择该主题 我现在的代码: if (count($_POST) > 0) { if (strlen($_POST['topic']) < 1) $errorMsg = "Please select a topic."; else if (strlen($_POST['message']) < 1) $errorMsg = "You have

正如标题所说,我将如何做到这一点

因此,假设用户留下的消息为空,但选择了正确的主题,我希望在页面刷新后选择该主题

我现在的代码:

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>';
}
if(计数($\u POST)>0){
如果(strlen($_POST['topic'])<1)
$errorMsg=“请选择一个主题。”;
else if(strlen($_POST['message'])<1)
$errorMsg=“您必须输入一条消息。”;
}
$topics=mysql_query(“按名称排序从主题中选择名称ASC”);
echo'(选择主题)';
而($t=mysql\u fetch\u assoc($topics)){
echo'.htmlspecialchars($t['name'])。';
}

您已经知道,
selected=“selected”
将使该选项处于选中状态,因此您就快到了

想想这个,在你的循环中:

if ($_POST['topic'] == $t['id']) 
    $selected = ' selected="selected';
else 
    $selected = '';
echo '<option value="'.$t['id'].'"' . $selected.'>'.htmlspecialchars($t['name']).'</option>';
if($\u POST['topic']==$t['id']))
$selected='selected=“selected';
其他的
$selected='';
echo'.htmlspecialchars($t['name'])。';

您能再添加一点代码来显示
标记的写入位置吗?另外,从选择菜单的第一个选项中删除
selected=“selected”
。当菜单中没有指定要选择的选项时,将始终选择第一项,因此该属性在那里是无用的。