Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'}},
    });
});
like image 468
JWDev Avatar asked Feb 02 '26 12:02

JWDev


1 Answers

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

like image 127
Hanky Panky Avatar answered Feb 04 '26 02:02

Hanky Panky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!