Php 将值保存在<;选择>;提交后的for循环

Php 将值保存在<;选择>;提交后的for循环,php,forms,Php,Forms,如果表单提交不完整,我将尝试使用for循环选项保留select中的值。它将值插入到数据库和函数中,但我不希望人们在提交表单不完整时再次填写所有for循环选择 if(!empty($_POST['height'])){ $height = $_POST['height']; } <select class="form-control selectpicker" name="height"> <option value="" disabled selected>

如果表单提交不完整,我将尝试使用for循环选项保留select中的值。它将值插入到数据库和函数中,但我不希望人们在提交表单不完整时再次填写所有for循环选择

if(!empty($_POST['height'])){
    $height = $_POST['height'];
}
<select class="form-control selectpicker" name="height">
    <option value="" disabled selected>Select Your Height</option>
        <?php
        for ($i=1; $i<=250; $i++)
        {?>
    <option value="<?php echo $i;?>"><?php echo $i;?>cm</option>
        <?php
        }
        ?>
</select>
if(!empty($\u POST['height'])){
$height=$_POST['height'];
}
选择你的身高

您在
$height
中增加了值。但在select中,您没有使用它来保留选定的值。按如下所示更改高度下拉列表-

if(!empty($_POST['height'])){
    $height = $_POST['height'];
}
<select class="form-control selectpicker" name="height">
    <option value="" disabled selected>Select Your Height</option>
        <?php
        for ($i=1; $i<=250; $i++)
        {?>
    <option value="<?php echo $i;?>" <?php if (isset($height) && $height==$i) echo "selected";?>><?php echo $i;?>cm</option>
        <?php
        }
        ?>
</select>
if(!empty($\u POST['height'])){
$height=$_POST['height'];
}
选择你的身高

我使用了完全相同的代码,但它对我不起作用,但我复制并粘贴了你的选项,它工作得非常完美。我想这肯定只是一个小小的打字错误。不管怎样,非常感谢你的帮助。
if(!empty($_POST['height'])){
    $height = $_POST['height'];
}
<select class="form-control selectpicker" name="height">
    <option value="" disabled selected>Select Your Height</option>
        <?php
        for ($i=1; $i<=250; $i++)
        {?>
    <option value="<?php echo $i;?>" <?php if (isset($height) && $height==$i) echo "selected";?>><?php echo $i;?>cm</option>
        <?php
        }
        ?>
</select>