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"
.