如果我不清楚地问我的问题或者问题中的措辞含糊不清,请道歉。
我目前正在编写一个电子商务网站的购物车,我一直在使用教程帮助我进入现阶段,但现在我被卡住了。
以下是允许客户更改数量的特定行以及旁边出现的“更改”按钮。
<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>
上面的代码是$ cartOutput的一部分 - 然后在DIV中回显。
以下是我尝试创建的按钮来更新所有数量,但现在只需刷新页面。
<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>
从字面上看,对此的任何帮助都会很棒!
编辑:
以下是第一段代码来自的完整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++;
}
&GT;
(还有一点,但这与Paypal Check out有关)
这应该是最后的编辑......这是处理调整数量的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
}
?>
答案 0 :(得分:1)
更新多个项目不依赖于按钮,而是依赖于保留值的输入字段。您应该为每个项目使用一个类型的字段,即如果您需要字段“item_to_adjust”和“quantity”,则每个项目都应包含这些字段。
看看你的表格,我认为这样做:
<form action="cart.php" method="post">
<?php
// loop through your items/item-ids/whatever you use
foreach($cart_items as $item) {
$item_id = $item['id']
echo '
<input name="quantity[]" type="text" class="form-element text-element form-element-small" value="' . $item['quantity'] . '" size="1" maxlength="2" />
<input name="item_to_adjust[]" type="hidden" value="' . $item_id . '" />
} ?>
<input name="adjustBtn" type="submit" value="change" />
</form>
你会得到:
<?php
$_POST['item_to_adjust'] = array([0] => 127, [1] => 6, [...]); // item-ids
$_POST['quantity'] = array([0] => 1, [1] => 15, [...]); // quantities
将item-id放到quantites中,您拥有所有项目,并可以使用您的代码调整数量:
<?php
// $cart_items_ne will be: array('127' => 1, '6' => 15, [...]);
$cart_items_new = array_combine($_POST['item_to_adjust'],$_POST['quantity']);
// update items
foreach($cart_items_new as $item_id => $quantity) {
// do stuff here
}
那是你搜索过的答案吗?