2

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

  • 搜索表格
  • 带有 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' ));

    回声 $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( '控制器' => '艺术家', '动作' => '索引', ), ));

    echo $this->Form->button('Unfeatured', array('name' => 'submit', 'value' => 'Unfeatured', 'type' => 'submit', 'url' => array( '控制器' => '艺术家', '动作' => '索引', ), ));

控制器:

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

我哪里错了?

4

2 回答 2

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
      }
   }
}
于 2012-12-01T17:05:40.147 回答
0

如果重定向不重要,您可以做一件事吗?只需进行 ajax 调用并传递单击按钮的值,然后根据需要从控制器重定向

于 2020-04-07T13:23:15.500 回答