2012-08-02 118 views
1

嗨,我有页面有两个按钮,应该允许人去添加页面,并继续添加到数据库,否则,如果他们点击另一个按钮,它会进入索引页面。cakephp重定向按钮

目前他们都只是将输入的信息添加到数据库并刷新页面,因此当一个人点击type_2按钮时,他们不会被带到索引页面。

这里是控制器

if ($this->Field->save($this->request->data)) 
{ 
    if($this->params['form']['type_1'] == 'type_1') 
     { 
      $this->Session->setFlash('The field has been saved'); 
      $this->redirect(array('controller' => 'Fields','action' => 'add')); 
     } 
     else if($this->params['form']['type_2'] == 'type_2') 
     { 
      $this->Session->setFlash('The template has been saved'); 
      $this->redirect(array('controller' => 'Templates','action' => 'index')); 
     } 


} 

这里的if语句

<?php 

echo $this->Form->create('Field', array('action'=>'add')); 


    echo $this->Form->create('Field', array('action'=>'add')); 
    echo $this->Form->input('name', array('label'=>'Name: ')); 
    echo $this->Form->input('description', array('label'=>'Description: ')); 
    echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text'));//this would be the conventional fk fieldname 
    echo $this->Form->button('Continue adding fields', array('name' => 'type', 'value' => 'type_1')); 
    echo $this->Form->button('Finish adding fields', array('name' => 'type', 'value' => 'type_2')); 
    echo $this->Form->end(); 


?> 

回答

0

不得不使用要求,而不是PARAMS

if ($this->Field->save($this->request->data)) 
{ 
    if($this->request->data['submit'] == "type_1") 
     { 
      $this->Session->setFlash('The field has been saved'); 
      $this->redirect(array('controller' => 'fields','action' => 'add')); 
     } 
     if($this->request->data['submit'] == "type_2") 
     { 
      $this->Session->setFlash('The template has been saved'); 
      $this->redirect(array('controller' => 'templates','action' => 'index')); 
     } 


} 
+0

你是完全正确的,你人忽略使用'$这个 - > params',而不是'$这个 - >请求 - > data'。 – pbond 2012-08-02 22:41:54

0

你如果条件是错误的,你检查指标['form']['type_1']['form']['type_2']认为,这应该是['form']['type']在两种情况,然后你检查它们的值,所以它变成:

if ($this->Field->save($this->request->data)) 
{ 
    if($this->params['form']['type'] == 'type_1') 
     { 
      $this->Session->setFlash('The field has been saved'); 
      $this->redirect(array('controller' => 'Fields','action' => 'add')); 
     } 
     else if($this->params['form']['type'] == 'type_2') 
     { 
      $this->Session->setFlash('The template has been saved'); 
      $this->redirect(array('controller' => 'Templates','action' => 'index')); 
     } 
} 
+0

目前,我们有,它仍然是不工作 – user1393064 2012-08-02 02:16:17