我有一个while循环,将选项值回显到选择输入,我想使用html的“ Selected”属性,以便在提交后保留选择的项目。
因此,如果用户选择了2,则该项目在选择中仍处于活动状态,而不是像当前那样重置它。
在循环中添加“已选择”将使用最后一项。我不知道如何将“已选择”应用于仅使用的项目。
<form action="#" method="post">
<select name="object"><?php
$query = "SELECT object_id FROM objects";
$select_object_id = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_object_id)) {
$object_description = $row['object_id'];
echo "<option value='$object_description'>{$object_description}</option>";
}
?>
</select>
<input type="submit" name="submit" value="Select" />
</form>
答案 0 :(得分:1)
<form action="#" method="post">
<select name="object"><?php
$query = "SELECT object_id FROM objects";
$select_object_id = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_object_id)) {
$object_description = $row['object_id'];
echo "<option ";
if($object_description == $_POST['object'])
echo 'selected ';
echo "value='$object_description'>{$object_description}</option>";
}
?>
</select>
<input type="submit" name="submit" value="Select" />
</form>