如何使用POST方法从数据库中获取选项来获取选择输入标记的值?

时间:2016-11-08 13:24:22

标签: php html forms select

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <select name="doctor">
    <?php 
    $con = mysqli_connect("---","---","---","---") or die("Can't Connect to the Database.");
    $sql = mysqli_query($con, "SELECT Title, Name, LastName FROM physician");
    while ($row = $sql->fetch_assoc()){
    echo "<option value=\"doctor1\">" . $row['Title'].' '.$row['Name'].' '.$row['LastName'] . "</option>";
    }

    ?>
    </select>
    &nbsp;<input type='submit' value="Filter"><br>
</form>

以上是我创建的表单。我用POST方法。此表单有一个选择输入标记,它的选项取自我的数据库。提交表单时,我需要使用$_POST['doctor']函数访问用户选择的值。但它并没有给我任何价值。谁能帮我?

1 个答案:

答案 0 :(得分:0)

如果&#34;医生&#34;中的每个条目的ID表存储在&#34; PhysicianID&#34;列中,您应该尝试以下代码段:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <select name="doctor">
        <?php 
            $con = mysqli_connect("---","---","---","---") or die("Can't Connect to the Database.");
            $sql = mysqli_query($con, "SELECT PhysicianID, Title, Name, LastName FROM physician");
            while ($row = $sql->fetch_assoc()){
                echo '<option value="'.$row['PhysicianID'].'">'.$row['Title'].' '.$row['Name'].' '.$row['LastName'].'</option>';
            }
        ?>
    </select>
    &nbsp;<input type='submit' value="Filter"><br>
</form>