Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter not receiving POST data

I already have a Codeigniter based RESTful api service which performs basic CRUD operations to the mySQL database. And now I'm trying to make a control panel with angularJS and Restangular.

I make a POST request like this:

HTML:

<form ng-submit="addUser()">
   <input type="text" name="name" ng-model="newUser.name" placeholder="Enter Your Name" />
   <input type="text" name="fact" ng-model="newUser.fact" placeholder="Enter A Fact" />
   <input class="pure-button" type="submit" value="Send" />
</form>

JS:

$scope.addUser = function ()
{
    var newuser = $scope.newUser;
    Restangular.one("user").post(newuser).then(function(data) {
      console.log(data);
    })
}

PHP (Using Codeigniter's inpu class):

$data = array(
    'name' => $this->input->post('name'),
    'fact' => $this->input->post('fact')
);

And then it writes $data array to the database. But in database, values are always 0, which means the $data['name'] and $data['fact'] are empty.

I think i should handle the data in a diffrent way in PHP but how can i do that? Or what is the type of POSTed data?

like image 782
SuperioREX Avatar asked Aug 12 '26 04:08

SuperioREX


1 Answers

try

$data = file_get_contents("php://input"); 
$data = json_decode($data,true); 

:)

like image 137
Whisher Avatar answered Aug 14 '26 18:08

Whisher