1

嗨,伙计们,我在表单中添加了下拉字段,但是在我提交表单后,如果有任何错误,下拉菜单会自行重置,如何在验证后保持价值,非常感谢您的帮助和想法这是我的代码

<td><select id="country" name="country"  style="width:150px;">
         <option value="-1">Select</option>
     <?php
$query = "SELECT country_id, name FROM countries ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
  echo "<option value=\"".$row['country_id']."\" >".$row['name']."</option>\n  ";
}
?>
</select></td>
<td><?php echo $form->error("country"); ?></td>
4

1 回答 1

2

通常,您将使用与selected当前选定值相关联的属性设置默认选项。所以在这种情况下,选项等于 的值$_POST['country']

while ($row = mysql_fetch_array($result))
{
    if ($row['country_id'] == $_POST['country'])
        $selected = "selected=\"selected\"";
    else
        $selected = "";
    echo "<option value=\"".$row['country_id']."\" $selected>".$row['name']."</option>\n ";
}

这将在适当的选项上呈现如下:

<option value="123" selected="selected">456</option>
于 2012-05-25T19:06:08.003 回答