What are some alternative methods to the header function for refreshing a PHP page while retaining form data?

When using the header function in PHP to refresh a page, form data is typically lost as the browser makes a new request to the server. To retain form data while refreshing the page, you can use alternative methods such as using JavaScript to submit the form or using a meta tag to automatically refresh the page.

// Using JavaScript to submit the form and retain form data
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Process form data

    // Output JavaScript to submit the form
    echo '<script>document.getElementById("myForm").submit();</script>';
} else {
    // Display the form
    echo '<form id="myForm" method="post" action="">
            <input type="text" name="data" value="' . ($_POST['data'] ?? '') . '">
            <input type="submit" value="Submit">
          </form>';
}