9

This is the situation:

I have a simple app made in Angular JS that comunicate with the server through an API made in codeigniter.

There is a login system in the app. When the user enter email and password, this data are sent to the server, if the email exist and the password match, it return true.

I have made many attempts but not figure out how can i do this properly.

This is the code:

The form:

<form role="form" method="post" novalidate ng-submit="submitForm()">
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" name="email" ng-model="user.name" placeholder="Enter email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" name="password" ng-model="user.password" placeholder="Password">
  </div>

  <button type="submit" class="btn btn-default">Submit</button>
</form>

This is the Angular js controller:

 $scope.authorized = false;

 $scope.user = {};

 $scope.submitForm = function() 
 {
    console.log("posting data....");

    $http({
        method : 'POST',
        url : 'http://127.0.0.1/api/main/login',
        headers: {'Content-Type': 'application/json'},
        data : JSON.stringify({email:$scope.user.email, password:$scope.user.password})
    }).success(function(data) {
         console.log(data);

        $scope.authorized = data; 

        if ($scope.authorized) { $location.path("memberArea"); };        

    });

}

In the codeigniter method have tried many things. Right now there is just this:

function login()
{
    print json_encode($_POST);
}

But i don't know if can receive the data into the $_POST because it seems to be empty.

So the question is:

How can i receive data in the codeigniter method? Is better to send as JSON and then json_decode? I have also tried json_decode($_POST, true); But was null. But if the data are not in $_POST where are? I am little confused..

Thank you for help!

EDIT:

Thanks guys for reply. That was one thing that have tried. But somehow is not working. Now for example the method is like this:

function login()
{
    $email = $this->input->post('email');
    var_dump($email);
    print json_encode($email);
}

But what is returned is a boolean false.

FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178

5 Answers5

14

thanks for the reply. solution is as follows

$obj=json_decode(file_get_contents('php://input'));

you can test it by

print_r(json_decode(file_get_contents('php://input')));
Karan Thakkar
  • 1,492
  • 2
  • 17
  • 24
  • https://stackoverflow.com/questions/46979306/receiving-an-object-as-an-ionic-parameter-to-a-php-controller?noredirect=1#comment80906165_46979306 – Ivan More Flores Oct 27 '17 at 16:26
3

$_POST will be empty in CodeIgniter because it purposely empties it for security reasons. You need to use $this->input->post(); instead.

CodeIgniter Input Class

Matt Asbury
  • 5,644
  • 2
  • 21
  • 29
2

Use:

$postData = $this->input->post();

It should give you an array with all the post data.

I also advise you to turn on XSS filtering.

Following is the documentation for the Codeigniter Input Class: http://ellislab.com/codeigniter/user-guide/libraries/input.html

grim
  • 6,669
  • 11
  • 38
  • 57
  • @johnnyfittizio I don't know anything with AngularJS but can you try to change your data attribute of your call to: `data : {email:$scope.user.email, password:$scope.user.password}` – grim May 14 '14 at 16:14
  • The most easy way is to pass the user object. data : $scope.user In this case data will be sent as a json object. but how can i receive it in codeigniter? – FrancescoMussi May 14 '14 at 16:21
  • By using the input class, Codeigniter should automatically perform a json_decode on the data you pass from your post. Have you tried to set the contentType and or dataType to JSON in your ajax call? – grim May 14 '14 at 16:27
  • Yes have set it (and edited question). But in the event i don't send data taken from a form, but fake string data. How can this be received in codeigniter. I think this is the point. – FrancescoMussi May 14 '14 at 16:33
  • By the way Angular is great! – FrancescoMussi May 14 '14 at 16:33
  • Are you still doing `$email = $this->input->post('email');` while still using `data : JSON.stringify({email:$scope.user.email, password:$scope.user.password})` ? This will definitely not work because you're sending a string but expecting an array which has a key named email. – grim May 14 '14 at 16:41
  • But if i leave data : $scope.user how can i retrieve it in codeigniter? – FrancescoMussi May 14 '14 at 16:47
  • If $scope.user is a JSON object, it will be translated into an array in Codeigniter when you use `$user = $this->input->post()` – grim May 14 '14 at 18:56
1

the data sent with the request is a name-value pairs so you should write some thing like that:

data : {"myformdata":JSON.stringify({email:$scope.user.email, password:$scope.user.password})}

and in code igniter you can recive the data :

$this->input->post("myformdata"); //should return what
                                  // that JSON.stringify returned
Majed DH
  • 1,251
  • 18
  • 35
1

Although this is already answered I quite often have this quick little utility included in many of my applications that need to accept both application/x-www-form-urlencoded and application/json POSTs.

if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') === 0 && stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE) {
    // POST is actually in json format, do an internal translation
    $_POST += json_decode(file_get_contents('php://input'), true);
}

After that point you can now just use the $_POST superglobal as you normally would, all the JSON data will be decoded for you.

Jason Larke
  • 5,289
  • 25
  • 28