如何从cakePHP中的输入表单将id和value传递给控制器​​?

时间:2018-03-22 01:31:13

标签: php cakephp httprequest cakephp-2.0

这是我的代码

查看(edit.ctp):

<?php echo $this->Form->create('Answer'); ?>
<?php echo $this->Form->input('Answer.0.value', array('class' => 'validate', 'type' => 'text', 'id' => 'Answer.0.id', 'label' => false)) ?>
<?php echo $this->Form->end('Edit Answer'); ?>

控制器:

public function edit($id) {
    $this->layout = 'request_processing_edit';
    $data = $this->Response->findById($id);
    if ($this->request->is(array('post', 'put'))) {
        $this->Response->id = $id;
        if ($this->Answer->save($this->request->data)) {
            $this->Session->setFlash('Answer Editted');
            $this->redirect('index');
        }
}

这就是$ this-&gt; request-&gt;数据数组的样子:

I need the id to be in the same array as value upon clicking submit in the view

有没有办法实现这一点而无需在控制器中构建阵列?在从视图/表单单击提交时,寻找一种在请求中传递id和值的方法。

2 个答案:

答案 0 :(得分:0)

通过在我的ctp文件中添加以下代码行找到解决方案:

<?php echo $this->Form->input('Answer.0.id', array('hidden' => true)) ?>

答案 1 :(得分:0)

所以,你可以用这种方式

在您的控制器中:

$data = $this->Post->findById($id);

if ($this->request->is(array('post', 'put'))) {
    $this->Answer->id = $id;
    if ($this->Answer->save($this->request->data)) {
         $this->Session->setFlash('Answer Editted');
        return $this->redirect(array('action' => 'index'));
    }
     $this->Session->setFlash('Answer Not Editted');
}

if (!$this->request->data) {
    $this->request->data = $data;

在您的视图文件中传递隐藏字段ID:

echo $this->Form->input('id', array('type' => 'hidden'));