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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.