Reputation: 320
I would like to being able to see a td value based on the row that a 'submit' button is in.
Here is my html code
<?php foreach($books as $booklist) : ?>
<tr>
<form action = "." method = "post">
<input type="hidden" name="action" value="addtocart" >
<td><img height="50" width="50" src=<?php echo "/../../product_manager/uploads/".$booklist['productimage']; ?>></td>
<td><?php echo $booklist['name']; ?></td>
<td><?php echo $booklist['version']; ?></td>
<td><?php echo $booklist['releaseDate']; ?></td>
<td><?php echo $booklist['price']; ?></td>
<td><?php echo $booklist['quantity']; ?></td>
<td><input name="submit" type="submit" value = 'Add to cart'/><td>
</form>
</tr>
<?php endforeach; ?>
And here is my controller
if($action == 'addtocart'){
echo $booklist['name'];
}
My problem is that it is echoing the last row only. If I echo version, price etc, it only echos the product named test. How can I get it to echo based on the submit button row? Also I'd like to keep it strictly php or html only.
Upvotes: 3
Views: 2436
Reputation: 11384
You can give the submit button an array based name such as name="submit[0]". That way you can use a counter variable in your loop to assign the value to the submit button and determine which button was pressed.
The above is my recommendation which you should use with a single form element. According to the HTML standards, a form should not be contained in a tr element, only in a td element. Therefore your form element should be contain your table and not the other way around.
Instead of a for loop, you can also try reading the value as:
$arr = array_keys($_POST['submit']);
echo $arr[0];
Upvotes: 2