【问题标题】:cakephp form action linkcakephp 表单操作链接
【发布时间】:2014-10-15 05:21:17
【问题描述】:

我正在使用 cakephp (2.4.7) 进行开发,但表单操作链接有问题。

我有一个带有编辑操作的 usersController。

public function edit($id = null, $slug = null) {
    if (!$id) {
       throw new NotFoundException(__('Invalid User'));
    }
    $user = $this->User->findById($id);
    if (!$user) {
       throw new NotFoundException(__('Invalid User'));
    }
    if ($this->request->is(array('post', 'put'))) {
       // Do stuff here
    }
    // Fill the form
    if (!$this->request->data) {
       $this->request->data = $user;
    }
}

使用此代码表单 ($this->create->('User'));在编辑视图中正确填写。但我在编辑视图中有另一个表单。

喜欢:

echo $this->Form->create(null, array(
     'url' => array('controller' => 'useraddresses', 'action' => 'add')
));
echo $this->Form->input('searchvalue');
echo $this->Form->hidden('country');
echo $this->Form->hidden('city');
echo $this->Form->end('save');

当我单击此表单中的发送按钮时,页面链接到 /useraddresses/add/2(2 是用户的 ID) 我已经用 firebug 调试了表单,并且在 action 参数中也是 /useraddresses/add/2。

我怎样才能解决这个问题?我会将表单发送到 /useraddresses/add,不带任何参数。

如果我在编辑操作中删除这段代码,则操作链接正确,但我的第一个表单没有填写。

// Fill the form
if (!$this->request->data) {
     $this->request->data = $user;
}

【问题讨论】:

  • // Input fields and Form->end('Send'); ???在此处粘贴您的输入字段代码

标签: php cakephp


【解决方案1】:

使用以下

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

代替你的

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

【讨论】:

  • 不起作用,第二种形式仍然链接到 /useraddresses/add/2 而不是 /useraddresses/add
  • 尝试使用类似 => echo $this->Form->create('searchUsers', array( 'url' => array('controller' => 'useraddresses', 'action' => 'add') )); 为 fornm 命名
  • 可能是会话,也可能是在 url 中添加/附加用户 ID 的 jquery 内容
猜你喜欢
  • 1970-01-01
  • 2015-10-30
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
相关资源
最近更新 更多