93

I need to see all of the POST results that are submitted to the server for testing.

What would be an example of how I can create a new file to submit to that will echo out all of the fields which were submitted with that form?

It's dynamic, so some fields may have a name/ID of field1, field2, field3, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zoolander
  • 2,293
  • 5
  • 27
  • 37
  • 5
    The reason why some of the solutions suggested doesn't look neat is because they rely on newlines, but are interpreted as HTML in the browser. Simply add `echo "
    ";` before the var_dump() or print_r().
    – kb. Feb 17 '12 at 17:36

7 Answers7

215

All the values are stored in the $_POST collection

<?php print_r($_POST); ?>

or if you want something fancier that is easier to read use a foreach loop to loop through the $_POST collection and print the values.

<table>
<?php 


    foreach ($_POST as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo $key;
        echo "</td>";
        echo "<td>";
        echo $value;
        echo "</td>";
        echo "</tr>";
    }


?>
</table>
Jared
  • 12,406
  • 1
  • 35
  • 39
24

You could try var_dump:

var_dump($_POST)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paco Valdez
  • 1,915
  • 14
  • 26
13

Simply:

<?php
    print_r($_POST);

    //Or:
    foreach ($_POST as $key => $value)
        echo $key.'='.$value.'<br />';
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicolas
  • 5,583
  • 1
  • 25
  • 37
9

You may mean something like this:

<?php
    $output = var_export($_POST, true);
    error_log($output, 0, "/path/to/file.log");
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Poni
  • 11,061
  • 25
  • 80
  • 121
  • $output = var_export($_POST, true); This is the best one, most of the time, echo it or write to file – Adamz Aug 29 '22 at 16:48
4

You could use something as simple as this

<?php
   print_r($_POST);
?>

This would make it a bit more viewable:

<?php
   echo str_replace('  ', '&nbsp; ', nl2br(print_r($_POST, true)));
?>
Vex
  • 1,489
  • 13
  • 20
4

You can definitely use var_dump, but you mentioned you are in front-end development. I am sure you would know this, but just as a reminder, use Firefox's Firebug or Chrome's / Internet Explorer's developers tool and check for the post. Post goes through hearders, and you should be able to check it from there too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2
if (! function_exists('d'))
{
    // Debugger
    function d($var, $exit = 0)
    {
        // Only output on localhost
        if ($_SERVER['HTTP_HOST'] != 'localhost')
        {
            return;
        }

        echo "\n[degug_output_BEGIN]<pre>\n";
        echo var_export($var, 1);
        echo "\n</pre>[degug_output_END]\n";

        if ($exit)
            exit;
    }
}

// Call:
d($_POST);

Bonus: Check debug_backtrace() too add tracing to your debugging.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Igor Parra
  • 10,214
  • 10
  • 69
  • 101