function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(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).ready(function(){
alert(getCookie('foo'));
setCookie('foo','test'); //just setting a cookie on page load
$('#bing').click(function(){
$('#frm1').submit();
});
});
HTML side(foo.phtml)
<a href="javascript:void(0);" id="bing">Go</a>
<form id="frm1" method="POST" action="/somecontroller/someaction">
<input type="hidden" name="foo_post" value="this is from post" />
</form>
//server side
public function someactionAction(){
$_COOKIE['foo'] = $this->_request->getPost('foo_post');
$this->_redirect('/somecontroller/foo');
}
My problem is on the very first load of a page the alert returns null since cookie is not set and if i refresh again,ill get an alert “test” but if i click on the anchor tag a form submit will occur and the cookie value is rewritten as “this is from post”,so obviously after the action back to the page ill get an alert of “this is from post” ,BUT im still getting an alert “test” ,The cookie value is not getting rewritten
In my mozilla cookie console there are two cookies with the name foo with values “test” and “this is from post”
“there are two cookies with the name foo”
If the name is identical, the domains must be different. Set both cookies using the same domain. Be aware that
example.netis different fromwww.example.netor.example.net.