首页 文章

cakephp通过按钮调用动作

提问于
浏览
2

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

  • 一个搜索表单

  • 一个以delte选项作为postLink的表

  • 2个按钮应该调用相同的动作onclick . 我的问题是,当我点击任何按钮时,不会触发帖子请求 . 下面是我的代码:view.ctp

echo $ this-> Form-> create('Search',array('type'=>'file','url'=> array('controller'=>'artists','action'=>'index', ),)); echo $ this-> Form-> input('name'); echo $ this-> Form-> end(array('label'=>'Search Artist','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-> button('Featured',array('name'=>'submit','value'=>'Featured','type'=>'submit','url'=> array( 'controller'=>'artists','action'=>'index',),));

echo $ this-> Form-> button('Unfeatured',array('name'=>'submit','value'=>'Unfeatured','type'=>'submit','url'=> array( 'controller'=>'artists','action'=>'index',),));

控制器:

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

我哪里错了?

1 回答

  • 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
          }
       }
    }
    

相关问题