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: "[email protected]",
   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?

1
  • 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? Commented Oct 31, 2019 at 21:36

1 Answer 1

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);        
}
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer Doru, it worked. Could you explain your code briefly? I want to learn. Thanks!
$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 codeigniter.com/user_guide/libraries/… stackoverflow.com/questions/8596216/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.