Skip to content
Advertisement

How to delete cookie on codeigniter

I don’t know how to delete a cookie. I want is when I submit a form. The cookie is also delete. I try the delete_cookie(“name”) but is not working. I think because the cookie I created by javascript. Please check my code to fix this problem.

This is my sample text field:

<input name="cargo_no" type="text"  class="validate[required]"  id="cargonumber" onchange="setCookie('cargonumberC', this.value, 365);"/>

and this is the javascript

function setCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();

    if (!nDays) 
        nDays=1;

    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
  }

document.addEventListener('DOMContentLoaded', function() {
  var themeSelect = document.getElementById('cargonumber');
  var selectedTheme = readCookie('cargonumberC');

  if (selectedTheme) {
    themeSelect.value = selectedTheme;
  }
});

Advertisement

Answer

Using Codeigniter, put it inside the save method of your controller:

Try:

delete_cookie('name', $domain, $path); 

Details on official documentation

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement