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

时间:2015-01-09 11:40:59

标签: php cakephp cancel-button

我正在学习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'));
        }
    }

}

?>

1 个答案:

答案 0 :(得分:1)

您正在视图中创建一个按钮。在Cakephp中,button-tag就像一个提交按钮。 因此,您必须创建一个链接(重定向到以前的站点):

$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')
);