dongqidi2799 2014-04-10 16:39
浏览 46
已采纳

如何使用javascript和Php清除其值的购物车?

I've created a shopping cart which works well.

Im having a problem clearing the cart, i want to use javascript to do this.

I want to use a button so when pressed it asks the user to confirm if they want to clear it, and if ok is pressed it is cleared.

So i have created the php code to clear my cart:

if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);

And when this is assigned to a link it clears the cart. For example:

<a href="cart.php?cmd=emptycart">Empty Cart</a>

However i want to change this to a button.

So far i have the following javascript code to give the validation but does not clear the cart.

<script language="javascript">

    function clear_cart(){
        if(confirm('Shopping cart will be cleared. Continue?')){
            document.form1.command.value='emptycart';
            document.form1.submit();
        }
}
</script>

The button:

<input type="button" value="Clear Cart" onclick="clear_cart()">

So my question is how do i get this to clear using this button and with the use of javascript or AJAX.

Thanks!

  • 写回答

2条回答 默认 最新

  • duanlv2788 2014-04-10 16:44
    关注

    This can easily be done with jQuery and AJAX.

    function clear_cart( ) {
        if( confirm( 'Shopping cart will be cleared. Continue?' ) ) {
            $.get( "cart.php?cmd=emptycart", function( data ) {
                alert( "Cart has been cleared!" );
            });
        }
    }
    

    More information on .get() is here: https://api.jquery.com/jQuery.get/

    If you prefer to not use jQuery, this might help: http://blog.mgechev.com/2011/07/21/ajax-jquery-beginners/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题