Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
I have a form with a single dropdown input. Upon successful submission, a cookie is set with the dropdown's value. This form is inside of a pages tag, in particular the 'k_paginated_top' section. I am using the cookie value to dictate the 'orderby' and 'order' parameters of the aforementioned pages tag. After the page reloads upon a successful submission, the pages tag still displays the cloned pages in the old ordering. However if I manually reload the page again, the proper cloned page ordering is displayed.
Code: Select all
<cms:set sorting="<cms:get_cookie 'sorting' />" />
<cms:if sorting >
<cms:if sorting='new' ><cms:set orderby_set='publish_date' /><cms:set order_set='desc' /></cms:if>
<cms:if sorting='old' ><cms:set orderby_set='publish_date' /><cms:set order_set='asc' /></cms:if>
<cms:if sorting='low' ><cms:set orderby_set='product_price' /><cms:set order_set='asc' /></cms:if>
<cms:if sorting='high' ><cms:set orderby_set='product_price' /><cms:set order_set='desc' /></cms:if>
</cms:if>


<cms:pages limit='15' paginate='1' orderby=orderby_set order=order_set>
<cms:if k_paginated_top >
<cms:form name='sorting_form' method='post' anchor='0' enctype="multipart/form-data">
<label for="sorting">Sort by:</label>
<cms:input type='dropdown' name='sorting' opt_values="Date Added: New to Old=new | Date Added: Old to New=old | Price: Low to High=low | Price: High to Low=high" opt_selected=sorting tabindex="1" onchange="this.form.submit()" />
<cms:if k_success >
<cms:set_cookie name='sorting' value=frm_sorting expire='0' />
</cms:if>
</cms:form>
</cms:if>
...
</cms:pages>

In summary, the problem I am experiencing is only on the first page reload (immediately after form submission), the 'orderby_set' and 'order_set' variables are not being set at their new values for the pages tag. Any ideas on a solution?

Update:
This sorting ability is more appropriately implemented with query strings than cookies. An added bonus is that you don't have to sacrifice caching.
Hi,

That is because when we set the cookie, the cookie simply gets send by the server to the browser. The browser will send back the cookie when the page is accessed any time next.

You see? Upon form submission we are only setting the cookie - not using the new info we are setting into the cookie (that we'll do when the cookie comes back on next visit).

I got it working this way -
Code: Select all
<cms:if "<cms:get_cookie 'sorting' />" >
   <cms:set sorting="<cms:get_cookie 'sorting' />" />
</cms:if>

<cms:form name='sorting_form' method='post' anchor='0' enctype="multipart/form-data">
   <label for="sorting">Sort by:</label>
   <cms:input type='dropdown' name='sorting' opt_values="Date Added: New to Old=new | Date Added: Old to New=old | Price: Low to High=low | Price: High to Low=high" opt_selected=sorting tabindex="1" onchange="this.form.submit()" />
   <cms:if k_success >
      <cms:set_cookie name='sorting' value=frm_sorting expire='0' />
      <cms:set sorting=frm_sorting 'global' />
   </cms:if>
</cms:form>

<cms:if sorting >
   <cms:if sorting='new' ><cms:set orderby_set='publish_date' /><cms:set order_set='desc' /></cms:if>
   <cms:if sorting='old' ><cms:set orderby_set='publish_date' /><cms:set order_set='asc' /></cms:if>
   <cms:if sorting='low' ><cms:set orderby_set='product_price' /><cms:set order_set='desc' /></cms:if>
   <cms:if sorting='high' ><cms:set orderby_set='product_price' /><cms:set order_set='asc' /></cms:if>
</cms:if>
     
<cms:pages limit='15' paginate='1' orderby=orderby_set order=order_set>
   ..
</cms:pages>

The variable 'sorting' is set either using an existing cookie or by the submitted form value (which also persists the choice in a cookie).

Hope this helps. Please let me know.
Thanks.
Thanks KK I got it to work now. I wasn't quite understanding how/when the cookie was being sent, leading me to make incorrect conclusions. Your post cleared up the process. Much appreciated.
Turns out this functionality only works if I am logged into the admin panel, when each page is being generated. If cached pages are served, it does not work as intended. Any ideas?
Hmm.. if we think about it, this shouldn't be surprising. Dynamic pages and caching will seldom go together. Couch is smart enough not to cache pages that are 'posted' (i.e. form submissions) but we are depending on cookies here which is complicating things.

The only workaround would be to turn off caching. i trust you are using v1.2RC. It has the feature of turning off caching on template basis too (in fact even on 'views' basis).
Please place the following tag in the relevant view of your template to prevent it from being cached -
Code: Select all
<cms:no_cache />

Hope this helps. Please let me know.
That makes sense, thanks for the fix.
Took me this long to realize the order by price (low to high and high to low) didn't actually work fully. The sorting was being done alphabetically, so for example with a low to high order setting, '100.00' would come before '50.00'.

As I understand it, the solution is to set the relevant editable region 'product_price' to search_type='decimal'. Is it intended behavior to for example convert '100.00' to '100' when inputted into the editable region? If so, I guess I will just have to use the 'number_format' tag.
Code: Select all
Is it intended behavior to for example convert '100.00' to '100' when inputted into the editable region? 

Cannot say it is unintentional but I think it'd be better to display the zeros for decimal search types. Have made a note of it and will get this fixed for the next release.

In the meanwhile, as you stated, the 'number_format' tag can be used for displaying the proper format.

Thanks.
8 posts Page 1 of 1
cron