JWDev
JWDev

Reputation: 856

PHP $_POST Not Returning Values

In PHP I have a form that submits data to a PHP script. The PHP script prints the values, using:

  $raw_post = file_get_contents('php://input');
  print_r($raw_post);
  print_r($_POST);
  print_r($_REQUEST);

All of these come back empty/null arrays EXCEPT $raw_post (aka, php://input).

Chrome's Developer Tools also show that the values have been submitted through the payload as a POST request and it is a status of 200 OK, yet PHP does not set them to the $_POST array at all.

Results in the $raw_post:

{"company_name":"test","primary_contact":"test","address":"test","function":"test","phone":"test","fax":"test","url":"test"}

Results in $_POST:

Array
(
)

Results in $_REQUEST:

Array
(
)

I am unable to find a solution to this issue ... could anybody help here?

The form is submitted from AngularJS to a PHP script.

New code (url-encoded):

app.factory('Companies', function($resource) {
    return $resource('/api.php/companies/:id', {id:''}, {
        'query': {method: 'GET', isArray: true},
        'view': {method: 'GET', isArray: true},
        'save': {
            method: 'POST',
            isArray: true,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}},
    });
});

Upvotes: 3

Views: 923

Answers (2)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

Try like this,

print_r(json_decode($raw_post));

Looks like you have json data.

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

Wow that sounds pretty simple doesnt it

$raw_post = file_get_contents('php://input');
print_r($raw_post); 

That already gives you the Posted JSON, just decode it :)

$values=json_decode($raw_post,true);

Now if you wanted to store all this data back in $_POST, you can simply do

$_POST=json_decode($raw_post,true);

That gives you your posted data.

Output

Array
(
    [company_name] => test
    [primary_contact] => test
    [address] => test
    [function] => test
    [phone] => test
    [fax] => test
    [url] => test
)

Fiddle

Upvotes: 1

Related Questions