1

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);});
    });

3 Answers3

1

You are setting the cookie when you generate your Javascript code

Barry Chapman
  • 6,438
  • 3
  • 31
  • 55
  • Set the cookie in your home/vote script url. The cookie will be passed back with the ajax response headers and get set that way. – Barry Chapman Sep 26 '12 at 06:57
0

PHP is a server side technology, so by the time you see the page PHP already done his job. So, there are no way to respond to client button clicks in php (except for reloading the page, but that's not the case in your situation). So, you need to remove setcookie from php and set your cookie with jQuery.

Refer to How to set/unset cookie with jQuery? for details.

Community
  • 1
  • 1
J0HN
  • 23,930
  • 5
  • 46
  • 83
  • 1
    That would work, but his best option would be to set the cookie in his `vote` action. That way, the cookie is passed back with the response headers and set appropriately. That way we can be sure that the cookie is only set on a successful vote. – Barry Chapman Sep 26 '12 at 06:57
0

Instead of using PHP to set the cookie, I'd suggest you to use jQuery itself. So, it'll be first using this.

And the function $("a.btn-success").click would also be including:

$.cookie("coupcookie", calledbyid, { expires : 7 });
hjpotter92
  • 71,576
  • 32
  • 131
  • 164
  • Two propblems. First, the cookie will get set regardless of a successful vote. The second is, according to the user, they want to maintain accessibility for users that have Javascript disabled. Your method would prevent the cookie from ever being set if JS were disabled. Setting it via response headers is going to be the best bet. – Barry Chapman Sep 26 '12 at 07:03
  • @BarryChapman Oh, I meant to include this inside the click-event. Also, if users don't have JS enabled, there won't be any `$("a.btn-success").click()` occuring. – hjpotter92 Sep 26 '12 at 07:05