0

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

1 Answer 1

1

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

6 Comments

Thank For your reply but how to use codeigniter form validation in your function add because server side form validation not applying i'm trying more time please tell me
You can do it on server side and send the validation success / failure message accordingly Ex. $_POST = json_decode(file_get_contents("php://input"), true); $this->form_validation->set_rules($data->name, 'Name', 'trim|required|min_length[3]'); if(!$this->form_validation->run()) { echo $result = '{"errors" : validation_errors()}'; }
@Nepster See how codeigniter do the validation But you need to get the post data and then you need to check validation and then return the result to client (angular) and show that message.
Thank You for reply but validation not applying error occur The first_name field is required
What problem your getting?
|

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.