Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在jQuery中处理购物车特性_Php_Jquery_Ajax - Fatal编程技术网

Php 在jQuery中处理购物车特性

Php 在jQuery中处理购物车特性,php,jquery,ajax,Php,Jquery,Ajax,我最近做了添加和删除购物车按钮,并在PHP中处理它们,但我希望在用户单击“添加到购物车”时“添加”一个div,当用户单击“从购物车中删除”时,div消失 如果我使用以下表单通过POST发送addToCart,我如何实现这一点: <form class="cartOperations" action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" class="add-to-cart

我最近做了添加和删除购物车按钮,并在PHP中处理它们,但我希望在用户单击“添加到购物车”时“添加”一个div,当用户单击“从购物车中删除”时,div消失

如果我使用以下表单通过POST发送addToCart,我如何实现这一点:

<form class="cartOperations" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">

<input type="submit" class="add-to-cart" name="removeFromCart" value="REMOVE">
<input type="submit" class="add-to-cart" name="addToCart" value="ADD TO BAG">
</form>

您需要使用ajax。像这样:

<input type="submit" onclick="delProductFromCart(id);" class="add-to-cart" name="removeFromCart" value="REMOVE">


并使用$\u会话进行数据存储,这不是一个好主意。使用php和数据库中的存储。

这个概念称为闪存消息


求你了。关于这个问题有很多信息。许多框架甚至对此有一个文档化的解决方案。参见示例。

答案:使用Javascript方法
window.setTimeout
。建议:使用
short\u open\u标签时要小心,因为默认情况下它们是关闭的。还要注意通过
PHP\u SELF
进行代码注入。为什么要使用GET?我想要一个职位request@SafiNajjar更改GET to POST??那么如何处理发布时的url?
<input type="submit" onclick="delProductFromCart(id);" class="add-to-cart" name="removeFromCart" value="REMOVE">
function delProductFromCart(priceid, url) {
$.ajax({
        type: "GET",
        dataType: 'html',
        url: "?ajax&delproductid="+priceid
      })
      .done(function( html ) {
          $(".block-cart-header").html( html );
          $(".block-cart-header").effect("highlight", {}, 1000);
          if (url!=undefined) { window.location = url; }
        });
}
function addDealToCart(priceid) {
$.ajax({
        type: "GET",
        dataType: 'html',
        url: "?ajax&dealid="+priceid
      })
      .done(function( html ) {
          $(".block-cart-header").html( html );
          $("html, body").animate({ scrollTop: 0 }, "fast", function(){$(".block-cart-header").effect("highlight", {}, 1000); });
        });