取消按钮将空行保存到 db cakephp

cancel button saves empty row to db cakephp

我正在学习 Cakephp 概念,下面是我在 cakephp 中的控制器的代码,保存成功但取消将空行添加到数据库。

<?php
   echo $this->Form->create('Customer');
    echo $this->Form->input('customer_name');
    .....
    echo $this->Form->button(
            'Save', 
            array('class' => 'button save')
        );

    echo $this->Form->button(
            'Cancel', 
            array('class' => 'button cancel')
        );

    echo $this->Form->end();

?>

下面是我的观点

<?php
public function add() {
    if ($this->request->is('post')) {

        $this->Customer->create();
        if ($this->Customer->save($this->request->data)) {
            $this->Session->setFlash(__('Your customer has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        elseif ($this->Customer->cancel('')) {
            $this->Session->setFlash(__('Customer info has not been saved'));
            return $this->redirect(array('action' => 'index'));
        }
    }

}

?>

您正在视图中创建按钮。在 Cakephp 中,按钮标签就像一个提交按钮。 因此,您必须要么创建一个 link(重定向到以前的站点):

$this->Html->link('Cancel', array('controller' => 'customer', 'action' => 'index'));

或阻止提交表单的按钮,例如通过 javascript (jQuery):

$('.button cancel').on('click', function(e)){
    e.preventDefault();
}

根据 this 站点,还有另一种选择:

echo $this->Form->button(
        'Cancel', 
        array('type' => 'button', 'class' => 'btn button cancel')
);