使用 Cakephp 表单助手提交数组
我有下面的 formhelper 代码,其中 $spot 是变量数组:
echo $form->create('Spot', array('controller' => 'spots', 'action' => 'view'));
echo $form->hidden('spotdata', array('value' => $spot));
echo $form->end('View');
当我在控制器中 print_r($this->data) 时,spotdata 为空。 formhelper 可以接受数组值吗?有什么办法可以做到这一点吗?请告知,谢谢!
I have the below formhelper code where $spot is an array of variables:
echo $form->create('Spot', array('controller' => 'spots', 'action' => 'view'));
echo $form->hidden('spotdata', array('value' => $spot));
echo $form->end('View');
When I print_r($this->data) in my controller, the spotdata is empty. Can the formhelper accept values that are arrays? Is there any way to do this? Please let me know, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到您正在将数据发送到视图。更惯用的做法是仅将 ID 发送到视图,然后可以从数据库重新加载数据。在这种情况下,一个链接就足够了:
如果您试图保持页面之间的状态,您可能会发现使用
SessionComponent
来执行此操作更容易、更安全(永远不要信任从客户端发回的数据) )。在您的控制器方法中,这就像:和
读回数据一样简单。
I notice you are sending the data to a view. It's more idiomatic to just send the ID to the view, and the data can be reloaded from the database. In which case a link would be enough:
If you are trying to keep the state between pages you may find it easier and more secure to use the
SessionComponent
to do this (never trust data sent back from the client). In your controller method, this is as easy as:and
to read back the data.
不,因为它回显 HTML 输入标记。该值必须是字符串或可以转换为字符串的内容。查看您的 HTML 源代码。
No, since it echoes an HTML input tag. The value has to be a string or something that can be cast as a string. View your HTML source.
您也可以 serialize( 而不是循环遍历数组并回显每个键) 数组并将其放置在单个隐藏输入中。然后你可以反序列化()它
Instead of looping through the array and echo'ing each key, you can also serialize() the array and place it in a single hidden input. Afterwards you can deserialize() it