кнопка отмены сохраняет пустую строку в базу данных

Я изучаю концепции 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'));
}
}

}

?>

0

Решение

Вы создаете кнопку в вашем представлении. В Cakephp кнопка-тег действует как кнопка отправки.
Поэтому вам нужно либо создать ссылку (перенаправить на предыдущий сайт):

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

или запретить кнопке отправить форму, например, через JavaScript (jQuery):

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

В соответствии с этот На сайте есть еще один вариант:

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector