Jump to content

Cookie update


budimir
 Share

Go to solution Solved by maxxd,

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>
Link to comment
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?

Link to comment
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
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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