I thought i did something smart when i serialize($userinfo) and saved the serialized data in my db... $userinfo is a plain array. but how do i get a hold of the data so my javascript can use it?
tried
var userinfo = <?=unserialize(GetUserInfo(userId));?>;
GetUserInfo is a global function that fetches the serialized string from the db.
asked May 10, 2011 at 11:04
Jason94
13.6k38 gold badges114 silver badges191 bronze badges
-
question is unclear?? please explain, may b this [article]( gist.github.com/242617) helps uxkeshav– xkeshav2011年05月10日 11:08:43 +00:00Commented May 10, 2011 at 11:08
1 Answer 1
Try the following:
var userinfo = <?php json_encode(GetUserInfo($user_id));?>;
JavaScript doesn't understand serialized PHP data but it does understand JSON as that is formated as a regular JavaScript object.
answered May 10, 2011 at 11:07
Karl Laurentius Roos
4,4391 gold badge35 silver badges42 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
geon
Make sure to utf8 encode al the data you pass into json_encode, since it only handles utf8. On an english website you might not notice, since all un-accented latin letters are the same in iso8859-1 and utf8, but it will stop working as soon as anyone enters an "ä", "ç" or "ฒ".
default