我创建了一个"添加到购物车"按钮,窗口位置设置为重新加载。但当我点击它时,它会无限地重新加载,或者永远不会停止。我只需要重新加载它,这样我仍然可以浏览更多的项目添加到我的购物车。我怎样才能把它设置在同一页上?
<?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.reload()</script>';
}
else {
echo '<script>alert("Products already added to cart")</script>';
echo '<script>window.location.reload()</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.reload()</script>';
}
}
}
}
如果您使用window.location.reload()
重新加载页面,它肯定会再次发布数据。
所以试着在
周围使用下面的工作$currentpage_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo '<script>window.location.href="'.$currentpage_url.'";</script>';