0

My searching skills seems to have failed me. I have this php object that I unserialize from a mysql entry, and I want to pass it as an argument to a javascript function, so it could compare the object with the values in a form. From what I gathered from my search, encoding the object as a json object would have done the trick, but when I'm doing a json_encode on the variable, it only result in {}.

Here is the relevant snippet of code:

<?php
$data = new Data();
$data = unserialize(base64_decode($rawdata));//Where $rawdata is the data retrieved from the mysql query.
/* using function such as $data->getName() to retrieve the relevant data */
?>

<form id="myform" action="#" method="post" onsubmit="compareEntry(<?=json_encode($data)?>)">

<!-- Different input and select field initialized with the php data -->

<input type="submit" onclick="compareEntry(<?=json_encode($data)?>)"/>
</form>
<!--<?=json_encode($data)?>--> 

I know that the php data is correctly retrieved from the database, as the values in the form are all correctly initialized. Only with the last html comment did I knew that I had an empty json object.

Here is an example of what print_r($data) returns (sensitive information edited):

(
    [m_path:private] => 
    [m_version:private] => REL_54
    [m_bugs:private] => Array
 *RECURSION*
    [m_targets:private] => Array
 *RECURSION*
    [m_symptoms:private] => Array
 *RECURSION*
    [m_exception:private] => Array
 *RECURSION*
    [m_instruction:private] => Array
 *RECURSION*
    [m_sources:private] => Array
 *RECURSION*
    [m_risks:private] => Array
 *RECURSION*
    [m_test:private] => Array
 *RECURSION*
    [m_contact:private] => Array
 *RECURSION*
)
1

Do I do something wrong? Is encoding to JSON the right approach in my scenario?

4
  • 1
    What does a print_r($data) result in? Commented Nov 24, 2010 at 10:10
  • @Pekka, I edited my question with the print_r result. Commented Nov 24, 2010 at 10:18
  • why are you using onsubmit and onclick for the same purpose i.e. compareEntry(<?=json_encode($data)?>) Commented Nov 24, 2010 at 10:36
  • @boota: To ensure that the same action is done by clicking on the button, and by pressing enter. I saw it done so in many question on SO. Should I do differently? Commented Nov 24, 2010 at 10:47

2 Answers 2

1

JSON is the correct way to do it. And basically json_encode/json_decode works well in that case. If it returns an empty object maybe there is a problem with the data you are trying to encode. the function expects the data to be in UTF-8, while PHP itself is still ISO-8859-1. So if you have e.g. special characters in some fields it may help if you convert these first with utf8_encode.

Sign up to request clarification or add additional context in comments.

Comments

0

Your object contains private properties only that won't be output by json_encode.

Also, there seems to be some sort of recursion going on, probably meaning that a member of each array is referencing the object itself (or something like that).

You will need to make some of the properties public, and probably also fix the recursion issues.

3 Comments

What if I need my properties to stay private as I don't want them to be accesible and changed by directing use the name of the property? Should I take the extracted value used to initialize my form and make a new object out of it? If it is the case, should I make it a php object, or can I directly build a json object out of it? Maybe another kind of "data container"? Should I edit all those questions in the OP?
@Eldros I would create a separate STDClass object, add the values to it, and json_encode() that.
Read about __toString() method also. It may be helpful for you. PHP.net link php.net/manual/en/language.oop5.magic.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.