避免在后期操作PHP中重定向

所以我有这个表格,当我点击提交输入时,我被重定向到动作URL。如何避免这种情况并保留在相同的URL中?

 ...
<form method="post" action="cart.php?action=add&pid='.$row["id"].'">
  <div class="cart-action" style="width: 100%;">
    <div class="input-group mb-3">
      <div class="input-group-prepend">
        <button class="btn btn-outline-primary js-btn-minus" type="button">−</button>
      </div>
      <input type="text" class="product-quantity form-control text-center"  name="quantity" value="1" size="2" placeholder="">
      <div class="input-group-append">
        <button class="btn btn-outline-primary js-btn-plus" type="button">+</button>
      </div>
    </div>
    <input type="submit" value="Agregar al carro" class="btnAddAction btn btn-outline-secondary" style="width: 100%;">
  </div>
</form>
...

这是将产品添加到购物车的代码

    //code for Cart
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    //code for adding product in cart
    case "add":
        if(!empty($_POST["quantity"])) {
            $pid=$_GET["pid"];
            $result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
              while($productByCode=mysqli_fetch_array($result)){
            $itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            }  else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    }
    break;

}
}

谢谢

评论
刺青
刺青

有几种方法可以做到这一点。这是一:

步骤1:添加包含当前页面URL的隐藏输入

<?php
   $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>

<input type="hidden" name="url" value=<?=$url;?>"

第2步:然后在您的cart.php页面中,添加产品后,您需要添加重定向,例如

if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    //code for adding product in cart
    case "add":
        if(!empty($_POST["quantity"])) {
            $pid=$_GET["pid"];
            $result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
              while($productByCode=mysqli_fetch_array($result)){
            $itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            }  else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    }
    header("Location: ".$_POST['url']);
    break;

}
}
点赞
评论
Bert
Bert

您应该使用ajax

$("form").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var url = form.attr('action');

    $.ajax({
           type: "POST",
           url: url,
           data: form.serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
    });
});
点赞
评论