I have objects in php that each represents a "item" and all info associated with it.

And when the user browses the page, these objects should be passed to javascript. Ideally, mirroring the same structure, so I can use Raphael to display each item and its info as separate shapes on my site.

However, how do you get an object from php to javascript?

share|improve this question

2 Answers

up vote 3 down vote accepted

You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from Javascript. Here are some basic steps:

  1. Convert the PHP object to an array.

  2. Using json_encode() to encode that PHP array.

  3. Pass JSON-encoded data to PHP

  4. Decode JSON from Javascript using eval() or you can use jQuery.parseJSON to do that.

This is an interesting tutorial about passing a Javascript object to a PHP object. You might find it useful by watching some other featured/related videos.

Hope this helps.

share|improve this answer

You will be using JSON to encode the PHP objects so that they can be accessed by Javascript. The specifics on how to do that depends on your application.

PHP has the functions json_encode and json_decode for this purpose.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.