What is the correct method for submitting a form in PHP to ensure data is properly processed?

When submitting a form in PHP, it is important to properly sanitize and validate the data to prevent security vulnerabilities and ensure data integrity. One common method to achieve this is by using PHP's filter_input function to retrieve and sanitize form inputs before processing them. This helps to prevent SQL injection, cross-site scripting, and other malicious attacks.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

    // Validate the data
    if (!empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Process the data
        // Your processing logic here
    } else {
        echo "Please enter a valid name and email address.";
    }
}
?>