raymond_feliciano Posted March 20, 2012 Share Posted March 20, 2012 I have a form which gets filled in with values from a session array. This form also has just one update button. If I have 3 items in my cart and I want to update just one of the items how would I pass the id of that product to my cart class? The problem I'm having is the id of the last product in the cart is passed so the update never works unless its the last item thats being updated. If I was to wrap each item in a form with an add update button it will work the way I want it to work but I really dont want to add extra buttons. Is there anyway to to wrap all the items in one form and get just the input values of the item whos quantity is being changed. Here is the cart form: <form action="index.php?view=update_cart" method="post" > <table id="items"> <thead> <tr> <th>Item</th> <th>Item Price</th> <th>Qty</th> <th>Subtotal</th> <!-- <th>Edit</th>--> </tr> </thead> <tbody> <?php foreach($cart->Cart() as $item):?> <!--<form action="index.php?view=update_cart" method="post" >--> <input type="hidden" id="prodId" name="prodId" value="<?php echo $item['Id']; ?>" /> <tr> <td><?php echo $item['Product']; ?></td> <td><?php echo '$'.$item['Price']; ?></td> <td><input type="text" size="2" name="qty" maxlength="2" value="<?php echo $item['Quantity']; ?>" onsubmit="this.value"/></td> <td><?php echo '$'.number_format($item['Price']*$item['Quantity'],2); ?></td> <!--td><input type="submit" name="update" value="Update" /></td--> </tr> <!--</form>--> <?php endforeach; ?> </tbody> </table> <p><input type="submit" name="update" value="Update" /></p> </form> When I debug my code and hover over the $_POST['prodId'] i will see the id of the last product. Any help will be appricated thanks. Quote Link to comment https://forums.phpfreaks.com/topic/259361-cart-form/ Share on other sites More sharing options...
rythemton Posted March 20, 2012 Share Posted March 20, 2012 The problem is that you are using the same names over again, so whenever the post is submitted, there is currently no way to distinguish which item is which. As PHP is processing the form, since they have the same name, it overwrites the old value. You need to give them different names and/or use arrays. <input type="hidden" id="prodId<?php echo $item['id']; ?>" name="prodId[]" value="<?php echo $item['Id']; ?>" /> <input type="text" size="2" name="qty<?php echo $item['id'];" maxlength="2" value="<?php echo $item['Quantity']; ?>" /> <!-- Use the following to get the values: --> <?php foreach( $_POST[ 'prodId' ] AS $product ) { $quantity = $_POST[ 'qty' . $product ]; } ?> I've use appending the ID to the name successfully. As long as the same product isn't listed multiple times, there shouldn't be any more conflicts. Quote Link to comment https://forums.phpfreaks.com/topic/259361-cart-form/#findComment-1329572 Share on other sites More sharing options...
raymond_feliciano Posted March 20, 2012 Author Share Posted March 20, 2012 Thanks it work like a charm had to edit my UpdateCart function but it works Quote Link to comment https://forums.phpfreaks.com/topic/259361-cart-form/#findComment-1329605 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.