Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在cakePHP中从输入表单向控制器传递id和值?_Php_Cakephp_Httprequest_Cakephp 2.0 - Fatal编程技术网

如何在cakePHP中从输入表单向控制器传递id和值?

如何在cakePHP中从输入表单向控制器传递id和值?,php,cakephp,httprequest,cakephp-2.0,Php,Cakephp,Httprequest,Cakephp 2.0,这是我的密码 查看(编辑.ctp): 这就是$This->request->data数组的外观: 有没有办法在不必在控制器中构建阵列的情况下实现这一点?在视图/表单中单击“提交”时,正在寻找将id和值同时传递到请求中的方法。通过在我的ctp文件中添加这行代码找到了解决方案: <?php echo $this->Form->input('Answer.0.id', array('hidden' => true)) ?> 因此,这里您可以使用这种方式 在控制器中:

这是我的密码

查看(编辑.ctp):

这就是$This->request->data数组的外观:


有没有办法在不必在控制器中构建阵列的情况下实现这一点?在视图/表单中单击“提交”时,正在寻找将id和值同时传递到请求中的方法。

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

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

因此,这里您可以使用这种方式

在控制器中:

$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'));
$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;
echo $this->Form->input('id', array('type' => 'hidden'));