Save data using AJAX and CakePHP

后端 未结 3 765
慢半拍i
慢半拍i 2020-12-15 10:48

I\'ve spent today researching how to save data using an ajax request in cakephp and have got now where, the documentation on the cakephp website seems a bit lacking for this

3条回答
  •  遥遥无期
    2020-12-15 11:35

     public function add_project() {
        $this->autoRender = false;
        $this->layout = 'ajax';
        if ($this->RequestHandler->isAjax()) {
            $this->Project->set($this->request->data);
            $this->request->data['Project']['user_id'] = $this->Session->read('Auth.User.id');
            $this->request->data['Project']['created_by'] = $this->Session->read('Auth.User.id');
            $this->request->data['Project']['updated_by'] = $this->Session->read('Auth.User.id');
            //$this->request->data['Skill']['accept_decline'] = 0;
            $this->User->set($this->request->data['Project']);
            Configure::write('debug', 0);
            if ($this->Project->validates(array('fieldList' => array('project_title', 'show_on', 'summary')))) {
    
                if ($this->Project->save($this->request->data, false)) {
                    $response['status'] = 'succses';
                    $response['message'] = 'data  sent';
                    echo json_encode($response);
                    exit();
                } else {
                    $response['status'] = 'error';
                    $response['model'] = 'Project';
                    $response['message'] = 'data not sent';
                    echo json_encode($response);
                    exit();
                }
            } else {
                $response['status'] = 'invalid';
                $response['model'] = 'Project';
                $errors = $this->Project->validationErrors;
                $response['errors'] = $errors;
                echo json_encode($response);
                exit();
            }
        }
    }
    

提交回复
热议问题