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);});
    });
5
  • Put up your click function too. Commented Sep 26, 2012 at 6:53
  • @BarryChapman. We are trying to build a website which functions almost the same on a phone too. Javascript is disabled on phones... Commented Sep 26, 2012 at 6:57
  • Javascript is disabled on phones? thats news to me! Commented Sep 26, 2012 at 6:58
  • If it is disabled, how will your jQuery work? Commented Sep 26, 2012 at 7:02
  • They are disabled on some phones. Isn't it?? Commented Sep 26, 2012 at 7:02

3 Answers 3

1

You are setting the cookie when you generate your Javascript code

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.