Jump to content
budimir

Cookie update

Recommended Posts

Hi everyone,

 

I'm strugling with javascript and updating cookie value, so I would like to ask for some help. I'm using bootstrap wizard and offer user some options to select. I would like to store that choice into cookie, but the problem I'm runing into is when user clicks on Cost and clicks Next on second step and then he decides to go back and make antoher choice and he chooses Custom, cookie is not updated with new value. I have to refresh page for couple of times before cookie changes it's value.

 

Below is my code I'm using to update and remove cookie so please help me and clarify me why cookie is not updated without refreshing screen for couple of times???

<script type='text/javascript'>
        $('input:radio').on('change', function() {
            if ($(this).val() === "Cost") {
                $.cookie('calc', 'Cost')
            } else {
                $.removeCookie('calc', { path: '/' });
            }

            if ($(this).val() === "Competition") {
            $.cookie('calc', 'Competition')
            } else {
                $.removeCookie('calc', { path: '/' });
            }

            if ($(this).val() === "Customer") {
                $.cookie('calc', 'Customer')
            } else {
                $.removeCookie('calc', { path: '/' });
            }

            if ($(this).val() === "Custom") {
                $.cookie('calc', 'Custom')
            } else {
                $.removeCookie('calc', { path: '/' });
            }
        });
        </script>

Share this post


Link to post
Share on other sites

The cookie parameters need to be the same every time you access it. That means including path:/ when setting the cookie, not just when removing it.

 

Are you sure that the cookie is not set? You've manually checked the value in your browser? So it's definitely not the case that the cookie is set correctly but the page is not showing the right... whatever it shows?

Share this post


Link to post
Share on other sites

Yes, I can see value which is displaying.

 

For example I select Cost and click to go to second step and there I can see it's displaying Cost, but when I go back to first step and select Customer and again go to second step the value didn't change from Cost. Strange thing is that I need to hit F5 and refresh browser exactly two times for new selection to work. I mean I hit F5 twice then make a new selection go to step two and I can see my new selection.

 

Same thing is hapening all the time? Do you have any idea why I need to refresh two times so I could get my new value?

Edited by budimir

Share this post


Link to post
Share on other sites

First, can you verify whether and when the cookie is being set? Forget the appearance of the page and look at the cookie directly through your browser's developer tools.

Share this post


Link to post
Share on other sites

Yes, I can see it. It is created. It doesn't say any time when is created, but I can see it is created when radio is selected.

Share this post


Link to post
Share on other sites

Oh, OK. Now I can see that cookie immediatly gets new value, but the page is not displaying it when I go to second step.

 

What do I need to do to get the value at the same moment when I get to second step?

Share this post


Link to post
Share on other sites

I'm using PHP to view cookie value, but obivously it's not working properly.

<?php echo "$COOKIE['calc']"; ?>

What javascript code would be to see cookie value in real time (without refreshing page)?

Share this post


Link to post
Share on other sites

For some reason I can't get it to work. Could you give me an example how to get cookie value and display it to see the value?

Share this post


Link to post
Share on other sites

The cookie superglobal in PHP is $_COOKIE[], not $COOKIE[]. So that may be part of the problem you're seeing. And as long as you're using the jQuery cookie plugin (which I assume you are from the snippet you posted), you should be able to read the contents of the cookie with:

console.log($.cookie('calc'));

See this SO question for more details. Also note that the jQuery cookie plugin has been deprecated in favor of a non-jQuery dependent version.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.