How do I print all POST results when a form is submitted?

How do I print all POST results when a form is submitted?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Logging Dynamic POST Data for Debugging

When you need to inspect every piece of data arriving from a form submission, especially when field names are dynamic like field1, field2, or field3, hardcoding a list of variables to debug is not scalable. You cannot write $echo($_POST['field1']); if the key changes on the next request. The goal is to iterate over the incoming data array and render it regardless of the specific keys present in that particular request.

The most direct approach in PHP involves accessing the $_POST superglobal, which contains an associative array of all submitted form fields. To handle dynamic field names, you can loop through this array using a foreach statement. This method automatically adapts to whatever keys are sent by the client, whether they are named sequentially or randomly.

Here is a basic implementation that echoes every key-value pair received:

<?php
// Ensure headers are sent before any output if you plan to use echo later
if (function_exists('header')) {
    header("Content-Type: text/plain; charset=utf-8");
}

// Iterate over all POST data dynamically
foreach ($_POST as $key => $value) {
    // Handle cases where the value might be an array (e.g., checkboxes or file uploads)
    if (is_array($value)) {
        echo "Field: {$key}\n";
        echo "Type: Array\n";
        print_r($value);
    } else {
        echo "Field: {$key} = {$value}\n";
    }
}

This code snippet is robust because it does not assume a specific structure for the input. It checks if a value is an array, which is common in forms containing multiple checkboxes or file uploads where only the filename and size are sent initially. By printing the raw key and handling both scalar and array types, you ensure no data is missed during your testing phase.

Storing Data Instead of Echoing

While echoing data directly to the browser is effective for quick debugging, it is often better practice to store this information in a log file or a database for later analysis. Direct output can be difficult to search through when dealing with high traffic or complex form submissions.

To log the data, you might replace the echo statements with file writing operations:

$logFile = 'debug_post_log.txt';

foreach ($_POST as $key => $value) {
    if (is_array($value)) {
        $logEntry = "[$key] Array\n";
        $logEntry .= json_encode($value, JSON_PRETTY_PRINT) . "\n";
    } else {
        $logEntry = sprintf("[%s] %s\n", $key, htmlspecialchars($value));
    }
    
    // Append to log file instead of printing
    file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
}

Using file_put_contents with the FILE_APPEND flag ensures that new data is added to the existing log without overwriting previous entries. Adding a timestamp at the beginning of the entry (outside the loop) would further help in correlating logs with specific events.

Security Considerations

Inspecting POST data is a legitimate debugging technique, but it must be handled with care. When displaying $_POST values directly to a user for debugging, always sanitize or escape the output. If you echo the raw value without escaping, an attacker could inject HTML tags or scripts if they control the input fields. Using htmlspecialchars() prevents Cross-Site Scripting (XSS) vulnerabilities when rendering data in an HTML context.

Furthermore, consider the performance implications. Logging large amounts of data to a file can quickly fill up disk space if not managed with rotation policies. For production environments, implementing log rotation or sending sensitive data only to specific debugging endpoints is crucial. If you are working within a Laravel application, you might prefer using the logger facade to write structured logs to your configured driver (like Monolog) rather than writing raw text to a file manually.

For more advanced logging needs within the Laravel ecosystem, resources like https://laravelcompany.com offer guidance on configuring logging channels and best practices for debugging without exposing sensitive application data.

Our network