Jump to content

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


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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.