Comments (4)
I don't know. Can you post exactly how you're setting and retrieving the cookie (the actual code)?
from cookies.
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.
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.
Yeah that makes sense: https://nystudio107.com/blog/twig-processing-order-and-scope
from cookies.
Related Issues (4)
- composer.json HOT 1
- Missing on Packagist? HOT 2
- Reading a JSON Cookie HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cookies.