我正在尝试使用cakephp 2.3中的ajax添加到数据库,但是不知道如何设置响应,但是对于用户来说我会使用其他数据
$this->set()
正常请求 视图文件:
echo $this->Form->create(); echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('phone');
echo $this->Form->input('message');
echo $this->Js->submit('Send Enquiry', array(
'before' => $this->Js->get('#sending')->effect('fadeIn'),
'success' => $this->Js->get('#sending')->effect('fadeOut'),
'update' => '#success',
'async' => true
));echo $this->Form->end();?>
,控制器功能是:
public function add() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
if($this->request->isAjax()){
$this->autoRender = false;
echo 'successful';
}else{
$this->Session->setFlash(__('The contact has been saved'));
$this->redirect(array('action' => 'index'));
}
} else {
$this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
}
}
}
答案 0 :(得分:0)
if($this->request->isAjax()){
$this->autoRender = false;
if($this->Contact->validates()){
}else{
$error = implode($this->Contact->validationErrors;
echo $error;
}
}
感谢所有相同的