2015-12-16 24 views
1

我已经制作了一个简单的购物车,其中有一个div,将显示在拥有购买物品的悬停上。问题是每次我点击add to cart按钮。它不会在实例中统计购物车物品。我需要做的就是自己刷新我的页面。任何建议或帮助将是可观的。页面刷新后count()函数更新php

代码

<div id="cartContainer"> 
    <div id="cart"> 
    <li style="color: #515151"> 
    <img id="cart_img" src="images/cart.png"> 
    Cart <span class='badge' id='comparison-count'> 
    <?php 
    if(isset($_SESSION['cart'])&& !empty($_SESSION['cart'])&& count($_SESSION['cart'])>0) 
    { 
    echo count($_SESSION['cart']); 
    } 
    else { 
    $cart_count=0; 
    echo $cart_count; 
    } 
?> 
    </span> 
</li> 
    <div id="sidebar"> 
    <?php 
    if(isset($_SESSION['cart'])&& !empty($_SESSION['cart'])){ 
    ?> 
    <table id="s_table"> 
    <?php 
    foreach($_SESSION['cart'] as $id => $value){ 
    //here it shows the item details 
    } 
    ?> 
    </table> 
    </div> 
    </div> 
    </div> 
+0

添加无缓存头? – developerwjk

+0

@developerwjk它将如何帮助我? – tabia

+0

服务器脚本不会在前端悬停时运行。 –

回答

1

我假设你是ajax呼叫做add-to-cart

现在您阿贾克斯成功做到这一点

var no_of_item = Number($("#comparison-count").html()); 
var new_no_of_item = no_of_item + no_of_item_purchased_in_last_add_to_cart; 
$("#comparison-count").html(new_no_of_item); 

如果你没有被阿贾克斯这样做,那么产品在php文件的开头呼应任何HTML标记前添加到购物车。

+0

我怎样才能得到这个'no_of_item_purchased_in_last_add_to_cart' – tabia

+0

@tabia应该有一个输入字段来输入要购买的项目号,否则它将是1。 –