0

我正在尝试在 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.'));
        }
    }
}
4

1 回答 1

0

抱歉,如果我的问题措辞有误,但我找到了问题的解决方案,我使用下面的代码片段来获取验证错误。

if($this->request->isAjax()){

        $this->autoRender = false;      
        if($this->Contact->validates()){

        }else{
        $error = implode($this->Contact->validationErrors;
        echo $error;


            }
        }

一样的感谢

于 2013-03-11T09:15:37.797 回答