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