What are the best practices for structuring PHP code to avoid unnecessary refreshes in form submissions?

To avoid unnecessary refreshes in form submissions, one should use the POST-redirect-GET pattern. This involves processing form submissions using POST method, then redirecting to a different page using GET method to prevent resubmission when the user refreshes the page. This helps in preventing duplicate form submissions and improves user experience.

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process form submission
    // Redirect to a different page to prevent resubmission
    header('Location: success.php');
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Form Submission</title>
</head>
<body>
    <form method="post">
        <!-- Form fields -->
        <input type="text" name="name" placeholder="Name">
        <input type="email" name="email" placeholder="Email">
        <button type="submit">Submit</button>
    </form>
</body>
</html>