Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Apologies if I don't ask my question clear enough or the wording is ambiguous in the question.

I am currently coding a shopping cart for an e-commerce website and I have been using tutorials/google to help get me to my current stage but now I am stuck.

What I am trying to do is make it so that when the user updates the quantities of various individual items, they are saved (the price then changes accordingly) at the click of one button. Right now, they save and calculate, but each button item has it's own "Change Quantity" button, whereas what I want is to just have on "Update Cart" button.

Currently, the following PHP block is used to check what the quantity is and to amend the quantity/price:

PHP
<?php 
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                  } // close if condition
              } // close while loop
    } // close foreach loop
}
?>


This block renders the actual cart which is then echo'd later:

PHP
<?php 

$cartOutput = "";
$minicartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
    $minicartOutput = "<p>You have no items in your cart</p>";
} else {
    // Start PayPal Checkout Button
    $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="xxxxxxxxx" value="xxxxxxxxxx">';
    // Start the For Each loop
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while ($row = mysql_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $shade = $row["shade"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "en_US");
        $pricetotal = money_format("%10.2n", $pricetotal);
        // Dynamic Checkout Btn Assembly
        $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
        <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $minicartOutput .= '<div class="form shopping-cart-form">

                                                        <form id="update_cart_form" method="post" action="/cart.php"  >



                                <ul class="listing cart-ul quick-cart-ul">
                                                            <li class="listing-li cart-li quick-cart-li first-li" data-quantity="1" data-id="149"> 
                                    <div class="box cart-item-box"> 
                                        <fieldset class="form-data-group">
                                            <legend class="form-legend">' . $product_name . '</legend>
                                            <dl class="meta-listing cart-item-dl">
                                                <dt class="meta-listing-dt cart-item-dt">Quantity</dt>
                                                <dd class="meta-listing-dd cart-item-dd">' . $each_item['quantity'] . '</dd>
                                                <dt class="meta-listing-dt cart-item-dt quick-cart-item-dt-price hide-me"><!--Price--></dt>
                                                <dd class="meta-listing-dd cart-item-dd quick-cart-item-dd-price">' . $pricetotal . '</dd>
                                            </dl>
                                            <div class="related cart-item-related">
                                                <div class="media img-media cart-item-img-media">
                                                    <figure>

                                                        <img width="56" height="56" src="../uploads/inventory/' . $item_id . '.jpg" alt="' . $product_name . '" />                                                      
                                                                                                            </figure>
                                                </div>
                                            </div>
                                        </fieldset>
                                    </div>
                                </li>


                                                        </ul>
                                                        </form>';
        $cartOutput .= '<div class="form shopping-cart-form">
                                <form id="update_cart_form" method="post" action="/cart.php"  >


                            <ul class="listing cart-ul">
                                                            <li class="listing-li cart-li first-li">
                                    <div class="box cart-item-box"> 
                                        <fieldset class="form-data-group">
                                            <legend class="form-legend">' . $product_name . '</legend>
                                            <dl class="meta-listing cart-item-dl cart-item-dl-price">
                                                <dt class="meta-listing-dt cart-item-dt">Price</dt>
                                                <dd class="meta-listing-dd cart-item-dd cart-item-dd-price">' . $pricetotal . '</dd>
                                            </dl>
                                            <dl class="meta-listing cart-item-dl">
                                                <dt class="meta-listing-dt cart-item-dt">Shade</dt>
                                                <dd class="meta-listing-dd cart-item-dd">' . $shade . '</dd>
                                                <dt class="meta-listing-dt cart-item-dt">Quantity</dt>
                                                <dd class="meta-listing-dd cart-item-dd">
                                                     <form action="cart.php" method="post">
                                                        <input name="quantity" type="text"  class="form-element text-element form-element-small" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /><input name="adjustBtn' . $item_id . '" type="submit" value="change" /><input name="item_to_adjust" type="hidden" value="' . $item_id . '" /></form> 
                                                </dd>
                                            </dl>
                                                <form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" class="form-label remove-from-cart-form-label delete-from-cart" type="submit" value="Remove From Cart" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form>
                                            <div class="related cart-item-related">
                                                <div class="media img-media cart-item-img-media">
                                                    <figure>

                                                           <img width="56" height="56" src="../uploads/inventory/' . $item_id . '.jpg" alt="' . $product_name . '" />

                                                                                                            </figure>
                                                </div>
                                            </div>
                                        </fieldset>
                                    </div>
                                </li>                               
                                                            </ul>

                                </form>';
        $i++; 
    } 


there is a bit more code at the end but it has to do with paypal verification and such.

The specific line which has the 'Change' button is:

PHP
<form action="cart.php" method="post">
<input name="quantity" type="text"  class="form-element text-element form-element-small" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form> 


and the Update Cart button is currently this:

PHP
<form action="cart.php" method="post">
<input class="btn sub-btn" name="adjustBtn' . $item_id . '" type="submit" value="Update">Update Cart</input>
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form>


Any help or advice on where I'm going wrong or what needs to be tweaked would be much appreciated!
Posted
Comments
Prasad Khandekar 28-Mar-13 14:35pm    
The HTML isn't right $cartOutput. It contains form within a form.
JioFreed 28-Mar-13 14:48pm    
Hi Prasad, what part should I be looking to change? And would that help with updating multiple quantities?
Prasad Khandekar 28-Mar-13 14:56pm    
In fact there are many parts in the above code that probably require a change. Apart from form within form you are outputting code for buttons in a look (for every row) If you want a single button then no need to put that in a loop. The best way I feel or rather I use is to look at the generated HTML source. Just copy the generated HTML in a editor like PSPAD and indent it properly you will be able to catch the error quickly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900