0

I am using codeigniter / grocerycrud, and when selecting a sort column, it remembers this order giving me the only option to re-sort the other direction (at least within the expiration date). I would like to create a button that deletes only the cookies relating to the sort. So far an example of a cookie would be...

hidden_sorting_4884b0e57a895f932a0a6f5657128eda

... and all of the cookies for sorting start with hidden_sorting_ so it makes sense to have a clear sorting button. Any ideas on if this is possible? A thorough research into built in features came up empty.

I can delete the full name with jQuery like so...

$.cookie("hidden_sorting_4884b0e57a895f932a0a6f5657128eda", null);

but having trouble with the prefix selection. In jQuery you can do things like...

$('input[products*="hidden_sorting_"]').val

if I was looking for input, but getting this in a cookie is what I need.

Shane
  • 1,629
  • 3
  • 23
  • 50
  • For anyone looking for solution in Javascript use the snippet below `var cookie_nm = document.cookie.split(/=[^;]*(?:;\s*|$)/);` `//cookies_prefix_to_remove is the cookies prefixes you’d like to remove` `//Delete all cookie that match the prefix` `for (var x = 0; x < cookie_nm.length; x++) {` `if (/^cookies_prefix_to_remove/.test(cookie_nm[x])) {` `document.cookie = cookie_nm[x] + '=-1; domain=your_cookie_domain.com; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/‘;` `}` `}` – Dexter Jan 01 '23 at 14:58

1 Answers1

1

Try this out:

<?php
session_start(); ob_start();
function destroyCookies($prefix){
  if(isset($_COOKIE)){
    foreach($_COOKIE as $i => $v){
      if(preg_match("/^$v/", $prefix)){
        setcookie($i, '', time()-3600); unset($_COOKIE[$i]);
      }
    }
  }
}
destroyCookies('hidden_sorting_');
ob_end_flush();
?>

You will have to use ob_start(), with PHP, to ensure you can set the cookie at any time.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • I was playing around with the use and no go. Any idea on accessing the value of this type of cookie instead? It is very simple, products_model. Each column with its own name. – Shane Jul 10 '13 at 23:21
  • Try displaying your cookies, like: `implode('; ', $_COOKIE);` to see if you really have the cookies. – StackSlave Jul 10 '13 at 23:49
  • I do get a list, like: `1; 100; asc; products_upc; ; products_model; 1; 50; asc; price_markup_9; ; ;`... – Shane Jul 11 '13 at 00:08
  • Do you see the cookie you are looking for? – StackSlave Jul 11 '13 at 00:26
  • Oh, that's the `products` cookie. I thought those were the names of your cookies. I believe I've fixed the problem. Try it again. – StackSlave Jul 11 '13 at 00:31