I have a problem regarding while loop. I have a option tag and hidden value inside the loop, the option tag work correctly for the dropdown list but the hidden value is not match with the selected dropdown list.
This is my code :
<?php
session_start();
require 'config.php';
$option1 = '';
$idseason='';
$season = '';
$query1 = "select id,description from codemaster_local where codeclass = 'season' and description like '%1st%' group by description";
$result1 = mysqli_query($doa,$query1);
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
if(isset($_POST['submit'])) {
$season = $_POST['season'];
$idseason = $_POST['id'];
echo "Season :"; echo $season;echo "<br>";
echo "ID : ";echo $idseason;
}
?>
<html>
<head>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/sty.css" rel="stylesheet" type="text/css">
<link href="style/dropdown.css" rel="stylesheet" type="text/css">
</head>
<body>
<form class="form-style-9" action="" method="post">
<table>
<tr><td><b>Season:</b></td>
<td> <select name="season" class="select-css">
<?php echo $option1; ?>
</select>
<?php echo $hidden; ?>
</td>
</tr>
</table><br>
<input type="submit" value="Search" name="submit">
</form>
</body>
</html>
For example, the id for 1st season 2005 should be 24800 but instead it choose the last id 30539. I select any of the season also still choose the last id. Is there any way to fix this?
$hidden
is being overwritten upon every loop within your while. Resulting in one hidden field with the name: id, containing the value of the last row id retrieved. That is the value you receive upon submission.
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."'>".$row1['description']."</option>";
$hidden = "<input type=hidden name=id value='".$row1['id']."'>";
}
I suggest you change the option
's value attribute to $row1['id']
:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['id']."'>".$row1['description']."</option>";
}
This way, $_POST['season']
will then hold the corresponding id for the season selected.
Try putting the id in the option tag instead of a hidden tag:
while($row1 = mysqli_fetch_array($result1)) {
$option1 .= "<option value='".$row1['description']."' id='".$row1['id']."'>".$row1['description']."</option>";
}