开发者

Submit Array with Cakephp Form Helper

开发者 https://www.devze.com 2023-02-23 07:49 出处:网络
I have the below formhelper code where $spot is an array of variables: echo $form->create(\'Spot\', array(\'controller\' => \'spots\', \'action\' => \'view\'));

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!


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:

echo $this->Html->link('View', 
    array('controller' => 'spots', 'action' => 'view', $spot['Spot']['id']));

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:

$this->Session->write('Spot.spotData', $spot);

and

$spot = $this->Session->read('Spot.spotData');

to read back the data.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消