What is the recommended method to pass data from an HTML form to a PHP script and display the result back on the same HTML page?

To pass data from an HTML form to a PHP script and display the result back on the same HTML page, you can use the POST method in the form submission and then process the form data in the PHP script. Once the PHP script processes the data, you can echo the result back to the HTML page using PHP.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $data = $_POST['data']; // Assuming 'data' is the name of the input field in the form

    // Process the data as needed
    $result = "Processed data: " . $data;
    
    // Echo the result back to the HTML page
    echo "<p>$result</p>";
}
?>

<!-- HTML form -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="data">
    <button type="submit">Submit</button>
</form>