SadSapp Posted April 14, 2008 Share Posted April 14, 2008 I'm stuck on something in a simple shopping cart application. I want to dynamically create a list of each item in the cart showing the picture, the item's name and a form to update the quantity of each item. When I update the quantity of an Item how do I know which item the new quantity is refering to? the problem with the code below is that the $_POST['updatedItem'] refers to the value if $item in the last iteration of the loop. But how do I track which form the new amount has been entered into? I think I'm not understanding something fundamental about forms. The page is self refering. cartaction.php: <?php if ($_POST['updatedItem']) { $itemUpdated = $_POST['updatedItem']; echo '<div id="header"> <p>Item quantity updated: '.$itemUpdated. '</p> </div>'; $_SESSION['cart'][$itemUpdated] = $_POST['amount']; } foreach ($_SESSION['cart'] as $item => $value){ if ($value > 0){ echo '<table> <tr> <td><img src="images/'.$item.'.jpg" alt="'.$item.'" /></td> <td><form action = "cartaction.php" method="post"> ' .$item .': <input type="text" size=2 name="amount" value="'.$_SESSION['cart'][$item].'"/> <input type="submit" value="Update This One Item"> <input type="hidden" name = "updatedItem" value = "'.$item.'"> </td> </tr> </table>'; } } http://ttphp.open.ac.uk/~rk2837/eca/ecapages/cartaction.php Link to comment https://forums.phpfreaks.com/topic/101037-creating-a-list-of-forms-with-a-loop-which-form-was-filled-in/ Share on other sites More sharing options...
rhodesa Posted April 14, 2008 Share Posted April 14, 2008 You're on the right track, but you are missing the close form tag </form> after the hidden input Link to comment https://forums.phpfreaks.com/topic/101037-creating-a-list-of-forms-with-a-loop-which-form-was-filled-in/#findComment-517052 Share on other sites More sharing options...
SadSapp Posted April 14, 2008 Author Share Posted April 14, 2008 Oh my God! thanks you so much. I can't believe it turned out to be something so trivial. I've lost hair over this. Thank you! I should really learn to check my tags first. Link to comment https://forums.phpfreaks.com/topic/101037-creating-a-list-of-forms-with-a-loop-which-form-was-filled-in/#findComment-517092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.