1

I have been searching for hours for a fix to my issue. I have found several similar issues but few of those fixes apply to me , and none have worked so far.

I am filling a database through a post form , but the array turns up empty:

print_r($_POST);

This results in

Array ( )

The full code to the index.php file:

    <?php

print_r($_POST);

?>

<!DOCTYPE html>
<html>
    <head>
        <title> Insert games </title>
    </head>
    <body>
        <form action="index.php" method="post">
            <label for="name"> Game Name </label>
            <input type="text" id="name" name="name">
            <input type="submit" name="submit">
        </form>
    </body>
</html>

So what this is supposed to do is send the name as $_POST value to the same file where I can print it.
It works perfectly fine with $_GET , and I am aware I can make this specific application without using $_POST. But later on in this project I would like to be able to use $_POST for more sensitive information.

There have been several debugging codes other posters have been asked to execute, these are the results I got when doing the same:

var_dump($_POST);

Results in

array(0) { }

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

Results in string(30) "name=TestGame&submit=Submit" apparently the post data IS visible through this , but I still have no idea how to use this data and why $_POST isn't working properly.

I am using Xampp on Windows 7 , Apache and MySQL are active.

I would be very grateful if someone could give me some advice

-Mick

chris85
  • 23,846
  • 7
  • 34
  • 51
Mickioo
  • 11
  • 3
  • Wierd, do you change PHP.ini file? – Ivijan Stefan Stipić Feb 07 '16 at 16:22
  • Do you have an `htaccess` file with any redirects/rewrites in place? – Andy Feb 07 '16 at 17:03
  • @IvijanStefanStipić I have seen with similar problems that there were issues with the php.ini file before. It was said that max_post_size (or something like it) should be 80M and "Read_Order" (again not 100% sure on the name) should be "EGPCS" Both of which were already in my php.ini – Mickioo Feb 07 '16 at 22:18
  • @Andy I'm not quite sure what you mean by that? Could you elaborate? – Mickioo Feb 07 '16 at 22:19
  • @A-2-A It results in once again nothing , only an empty array where the $_POST should have been. – Mickioo Feb 07 '16 at 22:22
  • @Mickioo It doesn't actually matter, I missed your `file_get_contents('php://input');` test. What does `var_dump($_REQUEST);` show when the form is posted? – Andy Feb 07 '16 at 22:36
  • 'array(0) { }' Unfortunately nothing as well. – Mickioo Feb 07 '16 at 22:42

3 Answers3

0

Try putting action="$_SERVER['PHP_SELF']" and press submit button.

Ognjen Babic
  • 131
  • 16
  • Your saying putting PHP code in the HTML without any tags? When I replace my action with "$_SERVER['PHP_SELF']" , it redirects me to a page with that name , which gives me a 404 error. Currently trying while printing that into the action code... – Mickioo Feb 07 '16 at 22:06
  • Yeah, you're right, you should add a php tags, like so: `action=""` – Ognjen Babic Feb 08 '16 at 06:06
  • Hmm it still has no result , I do not believe the issue is with the action as is works well when I use $_GET. – Mickioo Feb 08 '16 at 12:15
0

There is nothing wrong with your code.

Array ( [name] => test [submit] => Submit )

Check your Xampp settings. You can try action="/index.php" aswell.

JazzCat
  • 4,243
  • 1
  • 26
  • 40
  • What should I be looking for in my Xampp settings? When I add a "/" infront of my action it just redirects me to the directory above the php file. I'm fairly sure my action is fine? Because it does work using a $_GET method. – Mickioo Feb 07 '16 at 22:11
0

I have found a good temporary workaround provided by @felixsigl on a different post on this site: Link

It goes as follows:

function getRealPOST() {
$pairs = explode("&", file_get_contents("php://input"));
$vars = array();
foreach ($pairs as $pair) {
    $nv = explode("=", $pair);
    $name = urldecode($nv[0]);
    $value = urldecode($nv[1]);
    $vars[$name] = $value;
}
return $vars;
}

    $_POST = (getRealPOST());

This uses the php://input string and converts it into a $_POST array which can then be normally used.

I hope this helps someone with the same problem as I have.
If someone does find out why $_POST isnt working normally , please do let me know though.

Community
  • 1
  • 1
Mickioo
  • 11
  • 3