2

I am making an AngularJS application with a PHP backend, and am I attempting to persist some data to write into a database. I came across a hurdle I am attempting to jump over.

When I do a var_export($_POST) I get a blank (empty) array as a return.

However, when I do a $return json_decode(file_get_contents("php://input"), false); I get a full array with the values I expect?

How can I use the $_POST variable with the below?


My AngularJS code:

$scope.testPostRequest = function() {
    $http({
        method: 'POST',
        url: 'requestHandler.php',
        data: JSON.stringify($scope.scores), // pass in data as strings
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        } // set the headers so angular passing info as form data (not request payload)
    }).success(function(data) {
        console.log(data);
        if (!data.success) {
            // if not successful, bind errors to error variables
        } else {
            // if successful, bind success message to message
            $scope.message = data.message;
        }
    });
}

My entire PHP code (I just comment out the return I am interested in).

//$data = json_decode(file_get_contents("php://input"), false);
//return var_export($data);
return var_export($_POST);
Magnum
  • 1,555
  • 4
  • 18
  • 39
  • have you tried with a "echo" in case of "return" ? – meriadec Apr 13 '14 at 09:55
  • not getting the data which is sending from client side `data: JSON.stringify($scope.scores),` , is that your problem – Nidhish Krishnan Apr 13 '14 at 10:45
  • The problem is that `data` needs to hold form-urlencoded data (which Angular won't do for you). Available options: 1. manually urlencode the data (in a string of the form `param1=value1&param2=value2...`) 2. Use **[`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)** (needs special handling), but it is not supported in IE9 and earlier. 3. If you are using jQuery, you can use `$.param($scope.scores)`. Why use `$_POST` anyway ? Why not just use JSON ? – gkalpak Apr 13 '14 at 12:21
  • @dawuut Yes, I have played around with variations on the PHP side to ensure if $_POST variables aren't set accordingly. – Magnum Apr 13 '14 at 23:30

2 Answers2

1

ExpertSystem is right about how the data needs to be formatted.

Not ideal, but jQuery may hold the solution. If you have a reference to jQuery you can set data to:

data: $.param($scope.scores);

More details from here: http://jeecookbook.blogspot.com/2013/03/angularjs-trick-posting-data-using-url.html

Taken from this post: How can I post data as form data instead of a request payload?

Community
  • 1
  • 1
Ryan Taylor
  • 348
  • 3
  • 8
  • I needed to do `$.param(JSON.stringify($scope.scores))` instead. The above gives me an array of a single undefined element within it. – Magnum Apr 14 '14 at 05:59
1

Thank you to Ryan Taylor for his answer, and although I have selected his answer as the correct one I wanted to provide others looking at this post with a solution I came up with. Ryan's answer more directly answers my question though so kudos sir! Howevever, I am trying to avoid using jQuery for the purposes of my exercise:

Alternatively, I modified my back-end PHP code to do the following:

switch ($_SERVER['REQUEST_METHOD']) {
case 'POST':
    $data = json_decode(utf8_encode(file_get_contents("php://input")), false);
    if(!empty($data))
    {
        return var_export($data);
    }
    break;
default:
    header('HTTP/1.1 405 Method Not Allowed');
    break;
}

The above uses the raw PHP input to convert it into a JSON string. While my main objective was to try to make it populate the $_POST variable, this is the working solution I found without needing jQuery. Note: You will also notice that I am using the utf8_encode method because I read was necessary since certain non ASCII characters can make the JSON string fail to decode.

The above provided solution also avoids using jQuery in your application.

Magnum
  • 1,555
  • 4
  • 18
  • 39