2012-04-10 58 views
0

在我看來,我有一個提交和取消按鈕的表單。這兩個操作都會將我返回到我的索引頁。唯一的區別是提交會執行正常的數據提交併顯示消息「您的發票已更新」,而取消應取消更新並顯示「更新取消」。這裏的控制器代碼:CakePHP取消按鈕不會觸發

public function edit($id = null) { 
    $this->Invoice->id = $id; 
    $this->set('invoice', $this->Invoice->read(null, $id)); 
    //Check for $_GET 
    if ($this->request->is('get')) { 
     $this->request->data = $this->Invoice->read(); 
    } else { 
     // Bail if cancel button pressed 
     if (isset($this->params['form']['cancel'])) { 
      $this->Session->setFlash('Update canceled.'); 
      $this->redirect(array('action' => 'index')); 
     } else if ($this->Invoice->save($this->request->data)) { 
      $this->Session->setFlash('Your Invoice has been updated.'); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash('Unable to update your Invoice.'); 
     } 
    } 
} 

這裏是視圖:

<fieldset> 
<legend>Enter New Values</legend> 
<li><?php echo $this->Form->input('purchaseOrderNumber'); ?></li> 
<li><?php echo $this->Form->input('siteAddress'); ?></li> 
<li><?php echo $this->Form->input('siteCity'); ?></li> 
<li><?php echo $this->Form->input('siteState'); ?></li> 
<?php 
echo $this->Form->button('Submit Form', array('type' => 'submit')); 
echo $this->Form->submit('Cancel', array('div' => false, 'name' => 'cancel')); 
?> 

然而,無論哪個按鈕被按下時,它總是返回的第一個消息。它也執行數據庫提交。

我試着用Netbeans使用XDebug失敗,但這是一個不同時間的故事。通常我的錯誤對其他人來說很明顯。所以,我希望有人能讓我回到正軌。

+0

如果你把它放在你的else語句後面並且點擊取消,那麼輸出是什麼? '調試($這個 - > PARAMS);退出;' – Wylie 2012-04-10 19:16:07

+0

我剛剛使用鏈接返回索引結束 – 2012-04-12 17:10:41

回答

1

我只是在解決同樣的問題,我發現了一個解決方案,不需要簡單地鏈接回索引。在Wylie建議調試$this->params時,我注意到'form'數組沒有被創建。但是,有一個名爲'data'的數組,其中定義了'cancel'參數。 所以你在哪裏檢查,而不是

if (isset($this->params['form']['cancel'])) { 

使用

if (isset($this->params['data']['cancel'])) { 

哪個按鈕被按下,控制器,你會得到一個工作取消按鈕。