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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化