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

问题描述 投票:-1回答:3

所以我有此表单,当我单击Submit输入时,我被重定向到操作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;

}
}

谢谢

php html post submit action
3个回答
0
投票

您应该使用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.
           }
    });
});

0
投票

您可以使用headers将用户重定向回上一个URL。

header("Location: previous_page.php");

-1
投票

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

[步骤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;

}
}
© www.soinside.com 2019 - 2024. All rights reserved.