How can the message "Thank you for your registration" be displayed after a successful form submission in PHP, considering different submission methods like clicking a button or pressing Enter?

When a form is submitted in PHP, the page typically reloads, which can cause the message "Thank you for your registration" to be displayed only briefly or not at all. To ensure that the message is displayed after a successful form submission, you can use sessions to store a success message and then display it on the page.

<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process the form submission

    // Assuming the form submission is successful
    $_SESSION['success_message'] = "Thank you for your registration";
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}

// Display the success message if it exists
if (isset($_SESSION['success_message'])) {
    echo $_SESSION['success_message'];
    unset($_SESSION['success_message']);
}
?>