使用php设置选定属性

brqmpdu1  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(367)

我有一个while循环,它将选项值回显到select输入,我想使用html的selected属性,以便在提交后保留所选的项。
因此,如果用户选择了2,则该项在select中仍处于活动状态,而不是像当前那样重置。
将“selected”添加到循环中使用最后一项。我不知道如何将“选定”应用于只使用的项目。

<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>
7ivaypg9

7ivaypg91#

<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>

相关问题