cakephp通过按钮调用动作

时间:2012-12-01 07:59:06

标签: cakephp view controller action

我希望通过form->按钮调用控制器的操作。 我在网页上有以下内容:

  • 搜索表单
  • 将delte选项作为postLink的表格
  • 2个按钮,应该在onclick上调用相同的动作。 我的问题是,当我点击任何按钮时,不会触发帖子请求。以下是我的代码: view.ctp

    echo $ this-> Form-> create('Search',array(         'type'=> '文件',         'url'=>阵列(             'controller'=> “艺术家”,             'action'=> '指数',         )     )); echo $ this-> Form-> input('name'); echo $ this-> Form-> end(array(array)     'label'=> '搜索艺术家',     'class'=> 'btn btn-info controls'     ));

    echo $ this-> For ....
    echo '' . $this->Form->postlink('',
                        array('action' => 'delete',
                            $row['Artist']['id']),
                        array('confirm' => 'Are you sure?')
                    );
    

    echo $ this-> Form->按钮('精选',数组(                     'name'=> '提交',                     'value'=> “精选”,                     'type'=> '提交',                     'url'=>阵列(                 'controller'=> “艺术家”,                 'action'=> '指数',             )                 ));

    echo $ this-> Form->按钮('Unfeatured',数组(                 'name'=> '提交',                 'value'=> 'Unfeatured',                 'type'=> '提交',                 'url'=>阵列(             'controller'=> “艺术家”,             'action'=> '指数',         )             ));

控制器:

public function isFeatured() {
    if ($this->params->data['submit'] == 'Featured') {
        //code
    } else if($this->params->data['submit'] == 'Unfeatured') {
        //code
    }
    $this->redirect(array('action' => 'index'));
}
我在哪里错了?

2 个答案:

答案 0 :(得分:0)

您的表单声明并未将操作指向您的“isFeatured”'控制器中的功能。您应该将按钮重写为实际表单。按钮本身不会提交。

echo $this->Form->create('Search', array('action'=>'isFeatured'));
echo $this->Form->hidden('featured', array('value'=>'1'));
echo $this->Form->end('Featured');

echo $this->Form->create('Search', array('action'=>'isFeatured'));
echo $this->Form->hidden('featured', array('value'=>'0'));
echo $this->Form->end('Not Featured');

控制器:

public function isFeatured() {
   if($this->request->data){
      if($this->request->data['Search']['featured'] == '1'){
         //..Set the artist as featured
      }
      if($this->request->data['Search']['featured'] == '0'){
         //..Set the artist as not featured
      }
   }
}

答案 1 :(得分:0)

如果重定向不重要,可以做一个事情,只需进行ajax调用并传递clicked按钮的值,然后根据需要从控制器重定向