I was wondering if it was possible whether jquery can check if a cookie array exists or not.
My cookie name is ‘report’ and with php I echo it out like this:
echo '<pre>'; print_r($_COOKIE['report'][146]); echo '</pre>';
Which spits out something like this:
Array
(
[15128] => 15128
[13670] => 13670
)
This is where I’m hoping to get jquery to check if the above cookie array exists (or not), this is what I’ve got so far, but it’s not picking up the cookie array..
if ( $.cookie('report[146]') ) {
window.location = 'http://www.myurl.com/';
} else {
alert('Please make a selection.');
}
My form doesn’t refresh when someone ticks a checkbox, but it uses jquery to add the cookie (which works fine), but I can’t seem to get jquery to check if there are any cookies.
This is the code where it adds individual cookies to the array:
if ($(this).attr('checked')) {
$.cookie('report[146]['+thisID+']', thisID, { expires: 7, path: '/' });
} else {
$.cookie('report[146]['+thisID+']', thisID, { expires: -1, path: '/' });
};
Any help would be appreciated!
What you’re doing right now is setting cookie that is named
report[146]['+thisID+'](if for example thisID =135, then cookie name isreport[146][135]). Value of the cookie is not an array, the value of that cookie is value ofthisID.If you want cookie to have name
reportand it’s value should be array that at index 146 has valuethisID, do the following:set it:
Cookie value will now be set to (that’s the way array is serialized):
To read it: