Reputation: 136
I am trying to determine which bus has been picked for a reservation but unfortunately, I am not able to get the id properly
$sql = $db->prepare("SELECT * FROM Departure WHERE RouteId = 3");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr><td>";
echo $row['DepDate'];
echo "</td><td>75</td><td>";
echo $row['Seats'];
echo"</td><td> <input type='hidden' name='action' value='".$row['DepId']."' /><input id='";
echo $row['DepId'];
echo "' type='submit' name='submit' value='Pick'></td></tr>";
}
This code creates a table with the the busses from RouteId 3 For whatever reason, no matter which button I click, only the value of the last one gets set when I call $_POST['action']
Upvotes: 0
Views: 2377
Reputation: 8371
In Above case instead of getting Identifying bus with which Submit button is clicked you can create multiple forms in PHP Loop and pass hidden value to identify which Bus is selected.
Eg.
...
$sql = $db->prepare("SELECT * FROM Departure WHERE RouteId = 3");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<form action="selectBus.php">
<td><?php echo $row['DepDate']; ?></td>
<td>75</td><td><?php echo $row['Seats']; ?></td>
<td><input name='action' type="hidden" value="<?php echo $row['DepId'];?>"/>
<input type="submit" value="select" /></td>
</form>
</tr>
<?php
}
?>
Upvotes: 2