0

I have a problem to get a selected value and output it. Example i select 1001 from dropdown. When I echo it always return the value from first row wish is 1002.

this my code 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>

this is code from test.php

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

dropdown value

List of reservation ID

1 Answers1

1

This is of course a duplicate question.

EDIT:

After loop execution $gg will point to the last value in the list (1002 in this case). I believe you are trying to access the value of the user-chosen <option> of the <select> which can be done by:

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

In 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'];
}
Community
  • 1
  • 1
Isaiah
  • 484
  • 5
  • 16
  • the tag from html written in php. – Mohd Ariffin Mar 05 '17 at 06:20
  • Apologies for my misunderstanding of the question, I have reviewed and edited my answer. – Isaiah Mar 05 '17 at 06:27
  • Please clarify. Are you attempting to get the value of the ` – Isaiah Mar 05 '17 at 07:36
  • the – Mohd Ariffin Mar 05 '17 at 07:41
  • Correct. I have added a code example for what I intended. – Isaiah Mar 05 '17 at 07:43
  • @Isaiah the name should be `Reservation_ID` – mloureiro Mar 05 '17 at 07:53
  • I seen what you were going for in the edit (`reserve_id`), but the ` – Isaiah Mar 05 '17 at 07:57