0

I was trying to pass JSON to a PHP script in AngularJS, like so:

var testObj = {
  answers : {
    aa : 2,
    ab : 3
  }
};

var userAnswers = angular.toJson(testObj.answers);

$http.post("ajax/testAddObject.php?answers=" + userAnswers).
success(function(data){
    console.log("Result: ", data);
});

On the PHP side I was doing the following:

//... new PDO connection to DB
$answers = $_POST['answers'];

//some more stuff

The $answers variable was always empty. But then, almost randomly, I saw this question and the answer said:

$_POST will be empty when Content-Type: application/json is passed in headers

And, although my code was not exactly like the one from the question, I changed the $_POST[...] to $_GET[...] on the PHP code and it worked! Question is, why? What is the difference between one another? Because, from what I saw, there seems to be no big difference...

Community
  • 1
  • 1
flapas
  • 583
  • 1
  • 11
  • 25
  • 1
    http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them will provide some insight, as well as http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get – Martin Jul 04 '15 at 19:10
  • 1
    This is also a useful link for reading about the workings of GET/POST : http://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039 – Martin Jul 04 '15 at 19:13
  • Thanks a lot @Martin! already taking a look at them – flapas Jul 04 '15 at 19:18

3 Answers3

7

$http.post("ajax/testAddObject.php?answers=" + userAnswers).

Here you are sending answers as query parameters (GET) to testAddObject.php and not posting them (POST) - note the ? parameter at the end of testAddObject.php -

$_GET datas are passed as parameters in a URL. $_POST are not.

zeflex
  • 1,487
  • 1
  • 14
  • 29
  • He isnt even really using the post request it just hits the url with a parameter in the query string so it is possible to use get – Zach Jul 04 '15 at 19:28
  • 1
    That's why he gets nothing using $_POST as the parameter is not `posted` but added to url. You know that and I also. – zeflex Jul 04 '15 at 19:30
  • Yeah, I understand that now, also. I'm kinda noob in this particular topic.. Let me just ask you, then: if I did `$http.post("ajax/testAddObject.php", {answers : userAnswers})` then `$_POST['answers']` would work? – flapas Jul 04 '15 at 19:53
  • 1
    I never worked with angular js but related to the angular js documentation, your code should work. – zeflex Jul 04 '15 at 19:56
4

$_POST is populated with data from the body of the HTTP request if that data is formatted using one of the multipart or url-encoded formats.

$_GET is populated with data from the query string portion of the URL.

$_GET will be populated even if the request wasn't a GET request. It only cares if it was in the query string or not.


Since it was brought up in the comments. $_REQUEST contains the data from $_POST and the data from $_GET and the data from $_COOKIES. It's generally best avoided as it makes it possible to be surprised by data coming from places you don't expect.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I dug a little deeper into the problem and I found this article that explains that this kinda is an AngularJS "feature", actually.

By default angular.js sends all data in json. So if you do a POST request to a PHP code then the $_POST superglobal will not be populated.

Although my code was not the completely right, if I change the client side to:

$http.post("ajax/testAddObject.php?", {answers : userAnswers}) //...

and change the PHP accordingly, the $_POST[...] still returns nothing, which makes some sense now.

According to the same article there are two solutions, one on the client side and another on the server side

On the server you can parse input and then decode data from json:

$data = file_get_contents("php://input");
$postData = json_decode($data);`

On the client side the data can be sent in a way PHP expects it:

$http({
    url:url
    data : $.param(data),
    method : 'POST',
    headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(callback);
flapas
  • 583
  • 1
  • 11
  • 25