从mysli_fetch_array中获取选定的值并输出

时间:2017-03-05 06:06:44

标签: php mysql

我有一个问题是获取一个选定的值并输出它。示例我从下拉列表中选择 1001 。当我回显时,总是返回第一行的值,希望是 1002

这是我的代码 edit.php

<form id="form" action="test.php" method="post">
    <?php
    echo "<select name=\"Reservation ID\" form=\"form\">";
    while ($row = mysqli_fetch_array($result)) 
    {
        $gg = $row['reserve_id'];
         echo "<option value='" . $gg . "' name=\"reserve_id\">" . $gg . "</option>";
    }
    echo "</select>";
    $_SESSION['reserve'] = $gg;
    ?>
    <input type="submit" name="form" value="Submit">
</form>

这是来自 test.php

的代码
$y = $_SESSION['reserve'];
if(isset($_POST['form']))
{
  echo $y;
}

dropdown value

List of reservation ID

1 个答案:

答案 0 :(得分:1)

这当然是duplicate question

编辑

循环执行后$gg将指向列表中的 last 值(本例中为1002)。我相信您正在尝试访问<option>用户选择的<select>的值,该值可以通过以下方式完成:

edit.php

<form id="form" action="test.php" method="post">
    <?php
        echo "<select name=\"Reservation_ID\" form=\"form\">";
        while ($row = mysqli_fetch_array($result)) 
        {
            $gg = $row['reserve_id'];
            echo "<option value='" . $gg . "' name=\"reserve_id\">" . $gg . "</option>";
        }
        echo "</select>";
        $_SESSION['reserve'] = $gg;//this is not required to get <select> value, but may be relevant to what you are doing otherwise
    ?>
    <input type="submit" name="form" value="Submit">
</form>

test.php

$y = $_SESSION[''];//this is not required to get <select> value, but may be relevant to what you are doing otherwise
if(isset($_POST['form']))
{
    echo $_POST['Reservation_ID'];
}