this is what i have in my php file:
$session = $m->session;
$session is now:
object(stdClass)[31]
public 'id' => string '21112' (length=5)
public 'external_id' => string '' (length=0)
public 'sessiongroupid' => string '1843' (length=4)
public 'eventid' => string '5588' (length=4)
public 'order' => string '0' (length=1)
public 'name' => string 'Ferdau Conference' (length=17)
public 'description' => string 'Ferdau Conference' (length=17)
public 'starttime' => string '2013-04-18 18:00:00' (length=19)
public 'endtime' => string '2013-04-18 18:04:00' (length=19)
public 'speaker' => string '' (length=0)
public 'location' => string 'Waregem' (length=7)
public 'mapid' => string '0' (length=1)
public 'xpos' => string '0.000000' (length=8)
public 'ypos' => string '0.000000' (length=8)
public 'maptype' => string 'plan' (length=4)
public 'imageurl' => string '' (length=0)
public 'presentation' => string '' (length=0)
public 'organizer' => string '0' (length=1)
public 'twitter' => string '' (length=0)
public 'allowAddToFavorites' => string '0' (length=1)
public 'allowAddToAgenda' => string '0' (length=1)
public 'votes' => string '0' (length=1)
public 'url' => string 'http://ferdau.be' (length=16)
public 'venueid' => string '0' (length=1)
I want to send all the information to a function named save in my javascript file. Like this:
echo '<a onclick="save('.$session.')" style="cursor:pointer;" class="metacell">
<img src="'.buildUri("images/icons/favorite.png").'" width="16" />
<span>Add to favorites</span>
</a>';
When I try this I always get an error that only a string can be send. How can I convert this object to something I can send to my javascript function?
I've tried this with no result: $data = json_encode($session);
When I do $data = json_encode((array)$session)
I get this:
<a class="metacell" style="cursor:pointer;" ferdau.be","venueid":"0"})"="" \="" 18:04:00","speaker":"","location":"waregem","mapid":"0","xpos":"0.000000","ypos":"0.000000","maptype":"plan","imageurl":"","presentation":"","organizer":"0","twitter":"","allowaddtofavorites":"0","allowaddtoagenda":"0","votes":"0","url":"http:\="" 18:00:00","endtime":"2013-04-18="" conference","starttime":"2013-04-18="" conference","description":"ferdau="" id":"21112","external_id":"","sessiongroupid":"1843","eventid":"5588","order":"0","name":"ferdau="" onclick="save({">
PHP convert stdClass() object into JSON data $example_object = new stdClass(); $example_object->name = "Joe Bloggs"; $example_object->jobtitle = "Developer"; $json_data = json_encode((array) $example_object); print_r($json_data); exit; If there is a specific group or value that needs to be output as JSON data then use.
The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.
To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL.
$data = json_encode((array)$session);
is the right way to go.
I'm guessing that the html you are showing there is what you see in firebug/chrome dev tools/IE dev tools. That is what it looks like after the browser attempts to render it. Try looking at the raw source to see what the output actually looks like. Your problem at this point is caused by the double quotes from json_encode
causing the browser engine to consider the onclick
attribute closed before the end of the values from $data
.
You will need to do some mix of escaping double-quotes and/or using single-quotes in the HTML. I'd start with single-quotes, as that will be more simple. But you will also have to make sure none of the values in $data
include unescaped single quotes (as that would result in the same problem).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With