2012-04-10 60 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'])) { 

哪个按钮被按下,控制器,你会得到一个工作取消按钮。