Tanveer Khan
Tanveer Khan

Reputation: 77

Codeigniter Post data using AngularJS

I've built CRUD application using Codeigniter + AngularJS

how to post data in code-igniter controller

I am using this function get all POST data

$data = json_decode($this->input->raw_input_stream , TRUE);

But I want specific value using this function but response is NULL

$x = $this->input->input_stream('email', TRUE);

and one more question is how to apply code-igniter form validation for this $data

Thank You Please help me

Upvotes: 0

Views: 4477

Answers (1)

Mohit Tanwani
Mohit Tanwani

Reputation: 6628

Try following way.

I've assumed your code and provided an example, do the necessary changes as per your need.

Angular Js

console.log("posting data....");
$http({
    method: 'POST',
    url: '<?php echo base_url(); ?>user/add',
    headers: {'Content-Type': 'application/json'},
    data: JSON.stringify({name: $scope.name,city:$scope.city})
}).success(function (data) {
    console.log(data);
    $scope.message = data.status;
});

Controller action

public function add()
{
   // Here you will get data from angular ajax request in json format so you have to decode that json data you will get object array in $request variable
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $name = $request->name;
    $city = $request->city;
    $id = $this->user_model->AddUser($name,$city);
    if($id)
    {
     echo $result = '{"status" : "success"}';
    }else{
     echo $result = '{"status" : "failure"}';
    }
}

Upvotes: 1

Related Questions