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;
}
});
Cookie is a small piece of data sent from web server to store on client’s computer. CodeIgniter has one helper called “Cookie Helper” for cookie management. In the set_cookie () function, we can pass all the values using two ways. In the first way, only array can be passed and in the second way, individual parameters can also be passed.
The get_cookie () function is used to get the cookie that has been set using the set_cookie () function. The delete_cookie () function is used to delete the cookie (). Create a controller called Cookie_controller.php and save it at application/controller/Cookie_controller.php
This default is Lax. If you have set the SameSite to be an empty string and your default SameSite is also an empty string, your cookie will be given the Lax value. If the SameSite is set to None you need to make sure that Secure is also set to true.
An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user’s web browser. The browser may store it and send it back with later requests to the same server. Typically, it’s used to tell if two requests came from the same browser - keeping a user logged-in, for example.
Using Codeigniter, put it inside the save method of your controller:
Try:
delete_cookie('name', $domain, $path);
Details on official documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With