cakephp通过按钮调用操作
我希望通过表单->按钮调用控制器的操作。 我在网页中有以下内容:cakephp通过按钮调用操作,cakephp,view,controller,action,Cakephp,View,Controller,Action,我希望通过表单->按钮调用控制器的操作。 我在网页中有以下内容: 搜索表 具有delte选项作为postLink的表 2个按钮,在单击时应调用相同的操作。 我的问题是,当我点击任何一个按钮时,post请求都不会被触发。下面是我的代码: view.ctp echo$this->Form->create('Search',array( '类型'=>'文件', 'url'=>数组( “控制器”=>“艺术家”, “操作”=>“索引”, ), )); echo$this->Form->input('na
- 搜索表
- 具有delte选项作为postLink的表
- 2个按钮,在单击时应调用相同的操作。
我的问题是,当我点击任何一个按钮时,post请求都不会被触发。下面是我的代码:
view.ctp
echo$this->Form->create('Search',array(
'类型'=>'文件',
'url'=>数组(
“控制器”=>“艺术家”,
“操作”=>“索引”,
),
));
echo$this->Form->input('name');
echo$this->Form->end(数组)(
“标签”=>“搜索艺术家”,
“类”=>“btn btn信息控件”
));
echo$this->For。。。。
echo$this->Form->button('Featured',数组( '名称'=>'提交', '值'=>'特色', '类型'=>'提交', 'url'=>数组( “控制器”=>“艺术家”, “操作”=>“索引”, ), )); echo$this->Form->button('Unfeatured',数组( '名称'=>'提交', 'value'=>'Unfeatured', '类型'=>'提交', 'url'=>数组( “控制器”=>“艺术家”, “操作”=>“索引”, ), ));echo '' . $this->Form->postlink('', array('action' => 'delete', $row['Artist']['id']), array('confirm' => 'Are you sure?') );
public function isFeatured() {
if ($this->params->data['submit'] == 'Featured') {
//code
} else if($this->params->data['submit'] == 'Unfeatured') {
//code
}
$this->redirect(array('action' => 'index'));
}
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
}
}
}
我哪里做错了 表单声明没有将操作指向控制器中的“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->params->data['submit'] == 'Featured') {
//code
} else if($this->params->data['submit'] == 'Unfeatured') {
//code
}
$this->redirect(array('action' => 'index'));
}
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
}
}
}
如果重定向不重要,您可以做一件事吗?只需进行ajax调用并传递单击按钮的值,然后根据需要从控制器重定向即可,但控制器将如何获取请求数据。我的意思是,我在一个页面上有两个表单——一个是搜索表单,另一个是下面的表单,其中有两个按钮是特色按钮,而没有特色按钮。我无法为特色和非特色创建单独的表单。在您的两个单独表单中,如我上面所述。该值在特色中为1,在非特色中为0。因此,您可以在控制器中评估提交的数据,并根据评估结果执行保存。或者,如您所说,您可以创建两个单独的窗体、视图和控制器函数。不过看起来时间更长了。