2012-08-02 96 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