What are the potential pitfalls of using a counter to track filled fields in PHP form submissions?

One potential pitfall of using a counter to track filled fields in PHP form submissions is that it may not accurately reflect the number of fields filled if the form structure changes. To solve this issue, you can iterate through the form fields and check if each field has a value before incrementing the counter.

// Initialize counter
$filledFields = 0;

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Iterate through form fields
    foreach ($_POST as $key => $value) {
        // Check if field has a value
        if (!empty($value)) {
            $filledFields++;
        }
    }
}

// Output number of filled fields
echo "Number of filled fields: " . $filledFields;