1

I am working on a blog application with Codeigniter 3.1.8 and AngularJS v1.7.8.

The Dashboard of the application is "pure" Codeigniter, with Models, Controllers, and views, while the fronted is made up of JSONs managed and displayed by AngularJS.

I have been unable to add post comments from the frontend, via AngularJS.

My AngularJS controller looks like this:

 // Post comment
.controller('PostCommentController', ['$scope', '$http', '$routeParams', 
  function($scope, $http, $routeParams) {
    const slug = $routeParams.slug;
    $http.get('api/' + slug).then(function(response) {

        let post_id = response.data.post.id

        $scope.newComment = {
            slug: $routeParams.slug,
            post_id: post_id,
            name: $scope.name,
            email: $scope.email,
            comment: $scope.comment
        };

        $scope.createComment = function(){
            console.log($scope.newComment);
            $http.post('api/comments/create/' + post_id, $scope.newComment);
        };

    });
}])

My Codeigniter Comments controller (inside the API):

class Comments extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function create($post_slug){
        $data = json_decode(file_get_contents('php://input'), TRUE);
        $this->Comments_model->create_comment($post_id);        
    }

}

In the Comments_model model I have:

public function create_comment($post_id) {
    $data = [
        'post_id' => $post_id,
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'comment' => $this->input->post('comment'),
        'aproved' => 0,
        'created_at' => date('Y-m-d H:i:s')
    ];
    return $this->db->insert('comments', $data);
}

The line console.log($scope.newComment);, inside the createComment function returns an object with all the necessary "details":

{
   comment: "Test Angular",
   email: "razvan_zamfir80@yahoo.com",
   name: "Razvan Zamfir",
   post_id: "67",
   slug: "who-loves-a-butterfly"
}

Yet, I get a Failed to load resource: the server responded with a status of 500 (Internal Server Error) error for this url: api/comments/create/67 and the MySQL insert statement looks like this (I acn see it in the browser at the url /api/comments/create/67):

INSERT INTO `comments` (`post_id`, `name`, `email`, `comment`, `aproved`, `created_at`) VALUES (NULL, NULL, NULL, NULL, 0, '2019-10-31 22:06:01')

Where is my mistake?

Razvan Zamfir
  • 4,209
  • 6
  • 38
  • 252
  • 1
    If you send that json file, using another tool, like `postman` for example, pointing to the same endpoint, are you able to insert the data or you get the same error? – Hackerman Oct 31 '19 at 21:36

1 Answers1

1

you need to modify Comments_model::create_comment method:

public function create_comment($commentData) {
    $data = [
       'post_id' => $commentData['post_id'],
       'name' => $commentData['name'],
       'email' => $commentData['email'],
       'comment' => $commentData['comment'],
       'aproved' => 0,
       'created_at' => date('Y-m-d H:i:s')
    ];

    return $this->db->insert('comments', $data);
}

and call it in controller with $data:

public function create($post_slug){
    //also you can use $data = json_decode($this->input->raw_input_stream, TRUE);
    $data = json_decode(file_get_contents('php://input'), TRUE);
    $this->Comments_model->create_comment($data);        
}
Doru D.
  • 196
  • 2
  • 5
  • Great answer Doru, it worked. Could you explain your code briefly? I want to learn. Thanks! – Razvan Zamfir Nov 01 '19 at 18:16
  • $this->input->post() can be used usually only when you submit data with content-type multipart/form-data or application/x-www-form-urlencoded (clasical html form) but now your data are submitted by Angular with content-type application/json. that's why you need to get data from php://input stream and pass it to your model. more information you can find here https://codeigniter.com/user_guide/libraries/input.html#using-the-php-input-stream https://stackoverflow.com/questions/8596216/post-json-to-codeigniter-controller/15389426 – Doru D. Nov 01 '19 at 20:53