【问题标题】:Deleting a product from shopping cart从购物车中删除产品
【发布时间】:2015-05-16 13:33:41
【问题描述】:

我有一个几乎可以完美运行的购物车。但是,我希望能够删除产品而不必将数量设置为 0。因此购物车中的每个产品都有自己的删除按钮,名称 ='delete'。

现在我在页面顶部有这段代码。我已经有一个“更新购物车”按钮,它是代码的上半部分。代码的下半部分是删除按钮的代码。

<?php

	error_reporting(E_ALL); ini_set('display_errors', 1);
	
	if(isset($_POST['submit'])){
		
		foreach($_POST['quantity'] as $key => $val) {
			if($val==0) {
				unset($_SESSION['cart'][$key]);
			}else{
				$_SESSION['cart'][$key]['quantity']=$val;
			}
		}
		
	}
	
	if(isset($_POST['delete'])){
		
		foreach($_POST['quantity'] as $key => $val) {
				unset($_SESSION['cart'][$key]);
			}
		}

?>

<h2>Your shopping cart</h2>
	<p class="lead">Since the beginning, Movingscales™ has become one of the world's biggest scale models sellers with thousands of orders a year. We sell scale models in every kind of transportation, from motorcycles to planes. We don't just sell these products, but we are also involved in the production and design of the models. This is the reason why our products are premium-quality scale models. You can buy the products from our Movingscales™-collection through our online store or in one of our stores around the world.</p>
<form method="post" action="thecollection.php?page=cart">
    
	<table class="table table-striped">
	    
		<tr>
		    <th>Product</th>
		    <th>Quantity</th>
		    <th>Price</th>
		    <th>Total price</th>
			<th></th>
		</tr>
		
		<?php
		
			$sql="SELECT * FROM products WHERE productCode IN (";
					
					foreach($_SESSION['cart'] as $id => $value) {
						$sql.="'".$id."',";
					}
					
					$sql=substr($sql, 0, -1).") ORDER BY productName ASC";
					$query=mysql_query($sql) or die(mysql_error());
					$totalprice=0;
					while($row=mysql_fetch_array($query)){
						$subtotal=$_SESSION['cart'][$row['productCode']]['quantity']*$row['buyPrice'];
						$totalprice+=$subtotal;
					?>
						<tr class="registerform">
						    <td><?php echo $row['productName'] ?></td>
						    <td><input type="text" name="quantity[<?php echo $row['productCode'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['productCode']]['quantity'] ?>" /></td>
						    <td>€<?php echo $row['buyPrice'] ?></td>
						    <td>€<?php echo $_SESSION['cart'][$row['productCode']]['quantity']*$row['buyPrice'] ?></td>
							<td><button class="btn btn-login" type="submit" name="delete"><i class="fa fa-trash-o"></button></td>
						</tr>
					<?php
						
					}
		?>
					<tr>
					    <td colspan="5"><h4>Total price: <i>€<?php echo $totalprice ?></i></h4></td>
					</tr>
		
	</table>
	<br />
	<a href="thecollection.php?productline=all&scale=all&minimum=10&maximum=110&sortby=Name (A...Z)" class="btn btn-login">Continue shopping</a>
	<button class="btn btn-login" type="submit" name="submit">Update cart</button>
	<button class="btn btn-login" type="submit" name="submit">Checkout</button>

为什么它不起作用?每当您单击删除按钮时,我基本上都想取消它。

【问题讨论】:

  • html 是什么样子的?
  • @jeroen 我用 cart.php 页面的整个代码编辑了我的帖子。

标签: php sql shopping-cart cart


【解决方案1】:

如果每个产品都有自己的删除按钮,你怎么知道点击了哪个按钮?

要删除购物车中的产品,您需要知道选择了哪个产品。

您可以将删除按钮替换为链接:

<a href="/store/cart.php?act=delete&productid=$productid">delete</a>

或者

在表单中添加隐藏值:

<input type=hidden name=product_to_delete value=0>  
...
<input type=button name=deletebtn   onclick="javascript:document.cart_form.product_to_delete.value=$productid; document.cart_form.submit()"; >

或者

您可以为每个产品添加一个复选框,并为整个列表添加一个“删除”按钮,当您单击“删除”按钮时,您的 php 应该删除每个选定的产品 ID。

【讨论】:

  • 我已经用整个 cart.php 代码编辑了我的帖子。你能给我一个答案吗?我完全糊涂了。
猜你喜欢
  • 2020-02-17
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多