Jump to content

Cart form


raymond_feliciano

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.