How can one maintain the values of form elements after submission in PHP without using JavaScript?

When a form is submitted in PHP, the values entered by the user are typically lost. To maintain the values of form elements after submission without using JavaScript, you can store the values in PHP variables and then echo them back into the form fields in the HTML code. This way, when the form is submitted, the values will be retained in the form fields.

<?php
// Initialize variables to store form values
$name = '';
$email = '';

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve form values
    $name = $_POST['name'];
    $email = $_POST['email'];
}

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <input type="text" name="name" value="<?php echo $name; ?>" placeholder="Name">
    <input type="email" name="email" value="<?php echo $email; ?>" placeholder="Email">
    <button type="submit">Submit</button>
</form>