传递PHP对象作为javascript函数的参数

时间:2022-12-05 16:01:55

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 {}.

我的搜索技巧似乎让我失望了。我有一个php对象,我从mysql条目中反序列化,我想将它作为参数传递给javascript函数,因此它可以将对象与表单中的值进行比较。根据我从搜索中收集的内容,将对象编码为json对象就可以完成这个技巧,但是当我在变量上执行json_encode时,它只会导致{}。

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.

我知道从数据库中正确检索了php数据,因为表单中的值都已正确初始化。只有最后一个html注释才知道我有一个空的json对象。

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

以下是print_r($ data)返回的示例(已编辑的敏感信息):

(
    [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?

我做错了吗?在我的场景中编码为JSON是正确的方法吗?

2 个解决方案

#1


0  

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

您的对象仅包含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.

您需要公开一些属性,并且还可能修复递归问题。

#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.

JSON是正确的方法。基本上json_encode / json_decode在这种情况下效果很好。如果它返回一个空对象,则可能是您要编码的数据存在问题。该函数期望数据为UTF-8,而PHP本身仍为ISO-8859-1。所以如果你有例如某些字段中的特殊字符,如果您首先使用utf8_encode转换它们可能会有所帮助。

#1


0  

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

您的对象仅包含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.

您需要公开一些属性,并且还可能修复递归问题。

#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.

JSON是正确的方法。基本上json_encode / json_decode在这种情况下效果很好。如果它返回一个空对象,则可能是您要编码的数据存在问题。该函数期望数据为UTF-8,而PHP本身仍为ISO-8859-1。所以如果你有例如某些字段中的特殊字符,如果您首先使用utf8_encode转换它们可能会有所帮助。