当我点击"添加到购物车"有人可以帮助我制作窗口位置。或者删除项目按钮保留在同一页面上,这样我就可以轻松浏览其他项目而无需返回。
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "", "tut");
if (isset($_POST["add"])) {
if (isset($_SESSION["cart"])) {
$item_array_id = array_column($_SESSION["cart"], "product_id");
if (!in_array($_GET["id"], $item_array_id)) {
$count = count($_SESSION["cart"]);
$item_array = array(
'product_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["cart"][$count] = $item_array;
echo '<script>window.location="#"</script>';
} else {
echo '<script>alert("Products already added to cart")</script>';
echo '<script>window.location="#.php"</script>';
}
} else {
$item_array = array(
'product_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'product_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["cart"][0] = $item_array;
}
}
if (isset($_GET["action"])) {
if ($_GET["action"] == "delete") {
foreach ($_SESSION["cart"] as $keys => $values) {
if ($values["product_id"] == $_GET["id"]) {
unset($_SESSION["cart"][$keys]);
echo '<script>alert("Product has been removed")</script>';
echo '<script>window.location="#.php"</script>';
}
}
}
}
?>
这是我在每个页面下方显示购物车的表格:
<div style="clear:both"></div>
<h2>My Shopping Cart</h2>
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price Details</th>
<th width="15%">Order Total</th>
<th width="5%">Action</th>
</tr>
<?php
if (!empty($_SESSION["cart"])) {
$total = 0;
foreach ($_SESSION["cart"] as $keys => $values) {
?>
<tr>
<td><?php echo $values["item_name"]; ?></td>
<td><?php echo $values["item_quantity"] ?></td>
<td>$ <?php echo $values["product_price"]; ?></td>
<td>$ <?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?></td>
<td><a href="shop.php?action=delete&id=<?php echo $values["product_id"]; ?>"><span
class="text-danger">X</span></a></td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["product_price"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ <?php echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
</div>
答案 0 :(得分:0)
您可以重新加载页面。
<script>window.location.reload()</script>