Giter Club home page Giter Club logo

Comments (4)

khalwat avatar khalwat commented on August 27, 2024

I don't know. Can you post exactly how you're setting and retrieving the cookie (the actual code)?

from cookies.

 avatar commented on August 27, 2024

There is a guest entry submission with AJAX POST and redirect at the end.
The code is stripped-down.

AJAX post

<script type="text/javascript">
$('#add-to-cart').submit(function(ev) {
// Prevent the form from actually submitting
ev.preventDefault();

// Get the post data
var data = $(this).serialize();

// Add the CSRF Token
data[csrfTokenName] = csrfTokenValue;

// Send it to the server
$.post('/', data, function(response, textStatus, jqXHR) {

    if (response.success) {

    // Set countdown and redirection
    var count=2;
    var counter=setInterval(timer, 1000); //1000 will run it every 1 second

	function timer()
	{
	 count=count-1;
	 if (count <= 0)
	   {
              clearInterval(counter);

    //counter ended
    // HTTP redirect
    window.location.replace("{{ siteUrl }}uriA/uriB/{{ token }}");
	   }
	}

	 } else {
	   alert('Hmm..?');
	   }
     });

});
</script>

Redirected page
This page is only reached upon successful entry submission per user's session.
So here I set a simple secure cookie variable.

 {# Set cookie to store cart total. #}
 {% do craft.cookies.setSecure('cartTotal', total) %}

Than
In other template I simply check it if it has any length set like this.

{% if craft.cookies.getSecure('cartTotal')|length %}
    {{ craft.cookies.getSecure('cartTotal') }}
{% else %}
    0
{% endif %}

So when an entry is submitted and I get to redirect page destination the cookie cartTotal is already set in the browser but not retrieving it only upon manual page refresh.
That's the whole story :)

from cookies.

 avatar commented on August 27, 2024

Such rookie mistake. I had cookies variables defined after {% extends "" %} tag which inherited undefined cookie variable before It got set on first page redirect.

from cookies.

khalwat avatar khalwat commented on August 27, 2024

Yeah that makes sense: https://nystudio107.com/blog/twig-processing-order-and-scope

from cookies.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.