weixin_33739541 2016-02-11 08:51 采纳率: 0%
浏览 26

通过AJAX计数SetCookie

I am trying to implement "ADD TO SHORTLIST" via php/ajax

Here's my relevant codes

Calling ajax from this page :

<a id="shortlist" class="pull-right add-to-shortlist">Add to shortlist</a>
<p id="cookieShortList"><?=$_COOKIE['shortlist_count']?></p>

<script>

$("#shortlist").click(function(){ 
     $.ajax({
        type: "post",
        url: "ajaxcall.php",
        data:{spId : "111",prId : "222"},
        success: function(result) 
        {
            $('#cookieShortList').html(result);
        }
   });
});

</script>

POST DATA page:

$s_id = $_REQUEST['spId'];
$product_id = $_REQUEST['prId'];

setcookie("cookie_product[$s_id]", $product_id, time()+ (30),'/');
echo $shortlist_count_c = count($_COOKIE['cookie_product']);
if (isset($_COOKIE['cookie_product'][$s_id])) {

    echo "blah blah";
}

setcookie("shortlist_count", $shortlist_count_c, time()+ (30),'/');

The problem is I can see that cookie is being set on my browser under resources tab as "cookie_product[111]" => "222" on first click on "add to shortlist" but the count gets updated only after second click on "add to shortlist".

  • 写回答

1条回答 默认 最新

  • ~Onlooker 2016-02-11 09:02
    关注

    Read documentation

    Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array.

    Try this:

    $shortlist_count_c = count($_COOKIE['cookie_product']) + 1;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度