jquery cookie help needed (need help forming a cookie)
I was wondering if it was possible to create a cookie (using jquery.cookie) which looks like this in php:
$_COOKIE['124']['ctns'] => 12
$_COOKIE['124']['units'] => 2
OR
$_COOKIE['124'] => array( 'ctns' => 12, 'units' => 2 )
Currently I've created the cookie which looks like:
$_COOKIE['124-ctns'] => 12
$_COOKIE['124-units'] => 2
But I'm realising that's not going to work for what I need.
The code I'm using (jquery) is:
$.cookie('124-ct开发者_Go百科ns', 12, { path: '/' });
$.cookie('124-units', 2, { path: '/' });
Any help would be appreciated :)
You can't. Cookies ONLY store strings, not objects. You can convert the objects to JSON so the code looks something like
$_COOKIE['124'] => json_encode(array( 'ctns' => 12, 'units' => 2 ));
n124 = JSON.parse($.cookie('124'));
Then the variable n124 should be the object so you can get the variables like n124.ctns n124.units
To create multi-dimensional cookies in PHP:
setcookie("124[ctns]",12,time()+3600);
setcookie("124[units]",2,time()+3600);
The last parameter is the expiration time.
To access multi-dimensional cookies in PHP:
$ctns = $_COOKIE['124']['ctns']; // $ctns will equal 12
$units = $_COOKIE['124']['units']; // $units will equal 2
Quick, print_r($_COOKIE)
will output:
Array
(
[124] => Array
(
[ctns] => 12
[units] => 2
)
)
In short, yes, you can. The following shows a specific example from the PHP manual.
Example #3 setcookie() and arrays
This is how I do it:
<input name="cb[1]" type="checkbox" value = "1">
<input name="cb[2]" type="checkbox" value = "2">
<javascript>
$(":checkbox").click(function(){
name = $(this).attr('name');
val = $(this).val();
$.cookie(name, val);
});
</javascript>
Output when using print_r(cb):
[cb] => Array
(
[1] => 1
[2] => 2
)
But now my problem is how to access the stored cookies @.@
精彩评论