Pass associative data from php to javascript

Associate
Joined
26 Nov 2004
Posts
1,480
Location
Gran Canaria
Hi

What is the best way to pass associative data from php to javascript? I read that associative arrays in js are 'dangerous' and there really isn't anything obvious I can find.

Thanks.
 
take a look at JSON - similar functionality to an associative array. At what point are you going to try and pass data from PHP to javascript? You can use it to write Javascript, or if you are getting data asynchronously, you can use XML, HTML, I think PHP can use JSON too (never needed to do it) or you can pass strings.
 
Worked great, thanks. I needed to use the contents of a php query from an xmlhttp request to populate a select box. I created a string with all of the select values and option names, then used eval to create an object.

I don't really like sending syntax specific data from php and would rather send a pure data structure, if that makes sense.
 
my preferred method of transporting data like this is XML, just seems too fit for purpose to use anything else. When sending lots of other data, strings are pretty cumbersome, but whatever works for you, I guess!
 
It would seem that most developers would agree with you, though that may be resistance to change. Performance wise there doesn't seem to be much in it.
 
Back
Top Bottom