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
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity);
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) {
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
}
}
}
}
?>
This block renders the actual cart which is then echo'd later:
<?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 {
$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">';
$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);
$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'] . '"> ';
$product_id_array .= "$item_id-".$each_item['quantity'].",";
$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:
<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:
<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!