【问题标题】:Get selected value from mysli_fetch_array and output从 mysli_fetch_array 中获取选定的值并输出
【发布时间】:2017-03-05 06:06:43
【问题描述】:

我在获取选定值并将其输出时遇到问题。示例我从下拉列表中选择 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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这当然是duplicate question

    编辑

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

    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'];
    }
    

    【讨论】:

    • 标签。
    • 对我对问题的误解表示歉意,我已经查看并编辑了我的答案。
    • 请澄清。您是否尝试在 test.php 中获取在表单中选择的 &lt;option&gt; 的值?
    • 正确。我已经为我的意图添加了一个代码示例。
    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2023-03-25
    相关资源
    最近更新 更多