I have like and dislike buttons for multiple posts on my view.
But I want to restrict the voting and hence use cookies which lasts for 7 days.
And I use the $("a.btn-success").click(function()
function to calculate the success rate of the respective post and to set a cookie. But the php script that I use is setting the cookie even without the button being clicked.
<?php
$expire=time()+60*60*24*30;
setcookie("coupcookie", calledbyid, $expire);
?>
So if I just refresh the page, I can see that the cookie is set.
Could anyone please tell me what am I doing wrong here?
Thanks in advance.
edit
Here is my click function.
$("a.btn-success").click(function(){
var calledby = $(this);
var calledbyid=calledby.attr("id");
<?php
$expire=time()+60*60*24*30;
setcookie("coupcookie", calledbyid, $expire);
?>
var url = $(location).attr('href');
var sub = window.location.pathname.split('/');
alert("Hey button clicked "+calledbyid);
$.post(url.replace(sub[2]+'/'+sub[3],'')+"home/vote",{ "id" : calledbyid, "vote" : 1 }, function(data){
//alert("Hey post request completed");
$.get(url.replace(sub[2]+'/'+sub[3],'')+"home/getsuccess", {"id": calledbyid}, function(result){
$("#successrate"+calledbyid).html(result.concat('%'));
}, "text").error(function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);});
}, "text").error(function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);});
});