Jump to content

Creating a list of forms with a loop. which form was filled in?


SadSapp

Recommended Posts

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.