1

My problem is very common. I must be doing some silly mistake somewhere but I am not able to figure it out.

I am send my form data in serialized form but it is not coming to PHP at all.

Angular JS code:

saveForm: function() {
    var str = $('#feedbackForm').serializeArray();
    alert(JSON.stringify(str)); // here I am getting my data properly
    return $http({
        method  :'POST',
        url:'http://localhost/api?module=form&app_id=APP001&action=save&formid=2&user_id=3',
        data: str
    });
}

PHP

$log->info($_REQUEST); // I am getting all GET parameters correctly

tried this also

$log->info($_POST);

it is not printing my data. why?

runTarm
  • 11,537
  • 1
  • 37
  • 37
user1653773
  • 373
  • 1
  • 5
  • 18

1 Answers1

2

By default, the $http will send the data as application/json, which won't be recognized by the $_POST in PHP.

You have to choose either sending the data as form data like this:

return $http({
  method: 'POST',
  url: 'http://localhost/api?module=form&app_id=APP001&action=save&formid=2&user_id=3',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  data: str
});

Or don't use the $_POST but read and parse input directly in PHP like this:

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

Hope this helps.

runTarm
  • 11,537
  • 1
  • 37
  • 37
  • Can I put this PHP part as it is? or I need to replace anything? because I copied as it is..it is showing blank – user1653773 Aug 17 '14 at 19:09
  • If you choose the latter, you have to use the `$data` as an PHP json object like this `$data->name`, `$data->email`. – runTarm Aug 17 '14 at 19:14
  • it is not working still :(..The way I m serializing my form and sending it to php is correct?..can you give me some sample program..that would be really helpful. – user1653773 Aug 18 '14 at 01:50