Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
This probably isn't hard, but I couldn't find any explanations or examples anywhere about how to use the cookie tags, so here goes; I want to have a simple text link that upon clicking it creates a cookie

I tried the following;
Code: Select all
<a onclick="<cms:set_cookie 'cookiename' value='value'/>">create cookie</a>

...which didn't do anything :c
Hi,

There is a difference in the 'time-lines' of client-side code and server-side code.

In the code you posted, the "<cms:set_cookie .." part is run server-side and this happens much before the client-side part (i.e. the "<a onclick= ..") becomes visible to the visitor and she is able to click on the link.

So, with your code, the cookie would already have been set - regardless of whether or not the link is clicked on the client-side.

To make server-side code execute in response to a client-side action, you need to use 'forms'. Submitting the form (which could happen by clicking the link), would reach back to the server and you can then use <cms:set_cookie> at that time.
This is no different to, say, sending emails (which is a server-side action) when a contact form is submitted.

Please see our docs (http://docs.couchcms.com/concepts/forms.html) for details on how Couch handles forms.

Hope this helps.
thanks! I managed to get it working!

However, now I have no idea how to put that cookie to use... I want to use it to remember a setting, using the if tags, but I'm not sure how to do that, I tried what I could think of but nothing of it worked.
I want to use it to remember a setting, using the if tags, but I'm not sure how to do that,

Here is an example (assuming the name of the cookie you set earlier is 'cookiename') -
Code: Select all
<cms:set my_cookie_value="<cms:get_cookie 'cookiename' />" scope='global' /> 

<cms:if my_cookie_value = 'whatever_value_you_are_looking_for'>     
    // do something
</cms:if>   

If the value you set earlier can be only '0' or '1', the code above could be simplified to just this -
Code: Select all
<cms:if "<cms:get_cookie 'cookiename' />">
    // do something
</cms:if>

Hope it helps.
4 posts Page 1 of 1
cron