How to change the cookie in each click?

Asked

Viewed 49 times

1

I am doing an exercise to change the cookie at each click through ajax. I would like to set each click a different cookie according to the content that was sent by ajax. I am sending this Ajax to exchange the cookie.

<script>
jQuery(function(){
    /////////////////////////////////////////
    $(".cidade").on("click", function(event){
        ///////////////////////
        event.preventDefault();
        cidade = $(this).attr("id");

        $.ajax({
            url: "cookie.php",
            data: {
                cidade: cidade
            }
        });
    });
});
</script>

I would like your help to build a simple PHP script to exchange the cookie and have it presented in the next refresh. I did that which I think is pretty ugly and didn’t work. He takes the first cookie but not the others.

<?php
    if( isset($_REQUEST['cidade']) ){
        ///////////
        ob_start();
        setcookie("cidade", "", time()-3600);
        $cidade = $_REQUEST['cidade'];
        setcookie("cidade", $cidade);
        echo $cidade;
        /////////////
    }
?>
  • And it didn’t work?

  • It didn’t work. He picks up the first cookie but not the others.

  • As well as the first and not the others ?

  • 1

    Good afternoon, where exactly do you get the cookie? Because code node only has the part you set, you said it didn’t take the other cookies, but in your code only has a cookie the cidade.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.