What are best practices for handling form submission in PHP to avoid receiving a blank page?

When handling form submission in PHP, it is important to check if the form has been submitted before processing the data. This can be done by checking if the request method is POST and if the form fields are not empty. By implementing this check, you can avoid receiving a blank page when the form is submitted without any data.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST)) {
    // Process form data here
    // Example: 
    $name = $_POST['name'];
    $email = $_POST['email'];

    // Display success message or redirect to another page
    echo "Form submitted successfully!";
} else {
    // Display the form or any error messages
    echo "Please fill out the form.";
}
?>