【问题标题】:cakephp call action through buttoncakephp 通过按钮调用动作
【发布时间】:2012-12-01 07:59:06
【问题描述】:

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

  • 搜索表单
  • 带有 delte 选项作为 postLink 的表
  • 2 个按钮应该在点击时调用相同的操作。 我的问题是,当我单击任何按钮时,不会触发发布请求。下面是我的代码: 查看.ctp

    echo $this->Form->create('搜索', array( '类型' => '文件', 'url' => 数组( '控制器' => '艺术家', '动作' => '索引', ), )); echo $this->Form->input('name'); echo $this->Form->end(array( '标签' => '搜索艺术家', 'class' => 'btn btn-info 控件' ));

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

    echo $this->Form->button('精选', array( '姓名' => '提交', '价值' => '精选', '类型' => '提交', 'url' => 数组( '控制器' => '艺术家', '动作' => '索引', ), ));

    echo $this->Form->button('Unfeatured', array( '姓名' => '提交', '价值' => '无特色', '类型' => '提交', 'url' => 数组( '控制器' => '艺术家', '动作' => '索引', ), ));

控制器:

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

我哪里错了?

【问题讨论】:

    标签: cakephp view controller action


    【解决方案1】:

    您的表单声明并未将操作指向控制器中的“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
          }
       }
    }
    

    【讨论】:

    • 但是控制器将如何获取请求数据。我的意思是我在一个页面上有 2 个表单——一个搜索表单和一个下面有 2 个按钮的表单——有特色和没有特色。我无法为特色和非特色创建单独的表单。
    • 在你上面的 2 个单独的表格中。特色为 1,非特色为 0。因此,在控制器中,您评估提交的数据并根据评估执行保存。或者,就像您说的,您可以创建 2 个单独的表单、视图和控制器功能。不过似乎更长。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多