窗口位置重新加载无限PHP

时间:2016-09-21 12:42:19

标签: php html mysql

我创建了一个"添加到购物车"窗口位置设置按钮重新加载。但是当我点击它时,它会继续无限重装或永不停止。我只需要它重新加载所以我仍然可以浏览更多项目添加到我的购物车。如何在同一页面上设置它?

<?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>';
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用window.location.reload()重新加载页面,它肯定会再次发布数据。

所以尝试使用以下的工作

$currentpage_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo '<script>window.location.href="'.$currentpage_url.'";</script>';