简体   繁体   English

如何在php中将对象传递给隐藏形式?

[英]How do I pass an object into a hidden form in php?

I have an object called $todovalues and a form. 我有一个称为$ todovalues的对象和一种形式。 I need to pass the entirety of the todovalues object through the form. 我需要通过表单传递整个todovalues对象。

My form looks like - 我的表格看起来像-

<form action="" method="post">
<input class="todobuttons" type="submit" name="delete" value="delete">
<input class="todobuttons" type="submit" name="edit" value="edit">
<input type="hidden" name="post_item_info"
value="<?php echo serialize($todovalues); ?>">
</form>

I can access the values in $todovalues by using something like echo $todovalues->text. 我可以使用echo $ todovalues-> text之类的东西来访问$ todovalues中的值。

I have tried to use serialize, json_encode, and a mixture of both. 我尝试使用序列化,json_encode和两者的混合。 However, when I try to access the post value it is always empty. 但是,当我尝试访问post值时,它始终为空。

Is there a way to take my object and add it to the hidden input without having to create individual hidden inputs for each piece of the object that I want? 有没有一种方法可以将我的对象添加到隐藏输入中,而不必为我想要的每个对象创建单独的隐藏输入?

json_encode($todovalues);
json_encode(serialize($todovalues));
serialize($todovalues);

None of the above have been functional yet. 以上所有功能均无效。 Is there no simplified method to pass the whole of an object without iterating through the object first? 是否没有简化的方法来传递整个对象而不先遍历该对象? It just seems like a waste of space to have to create hidden inputs for each item in the object. 不得不为对象中的每个项目创建隐藏的输入似乎浪费了空间。

EDIT - 编辑-

When I use json_encode() I see the following so I know it SHOULD exist - 当我使用json_encode()时,我看到以下内容,因此我知道它应该存在-

<input type="hidden" name="post_item_info" value="{" id_auto":"3","id":"1","id_list":"1","completed":"0","incident_notes":"apple"}"="">

Possibly, you try to encode object. 您可能会尝试对对象进行编码。 Try encode an array: 尝试编码数组:

json_encode((array) $todovalues);

UPD. UPD。 Or, it can happens because your html looks like this: 或者,由于您的html如下所示,可能会发生这种情况:

<input value="{"id":3....}"> (a lot of ")

You can try base64_encode() 您可以尝试base64_encode()

The json_encode was breaking due to the fact that I created the input using double quotes. 由于我使用双引号创建了输入,因此json_encode中断了。

The system saw - 系统看到-

value="{" stuffGoesHere " value =“ {” stuffGoesHere“

I switched to single quotes - 我改用单引号-

value=' {"stuffGoesHere" ' 值='{“ stuffGoesHere”'

It works perfect now. 现在可以正常使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM