How can the PHP code be modified to correctly display user input values in a form?
When displaying user input values in a form using PHP, it's important to ensure that the values are properly escaped to prevent security vulnerabilities like Cross-Site Scripting (XSS) attacks. One way to do this is by using the htmlspecialchars function to encode the user input before displaying it in the form. This function will convert special characters to their HTML entities, making the input safe to display.
<?php
// Retrieve user input values
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '';
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '';
// Display form with user input values
echo '<form method="post">';
echo 'Name: <input type="text" name="name" value="' . $name . '"><br>';
echo 'Email: <input type="email" name="email" value="' . $email . '"><br>';
echo '<input type="submit" value="Submit">';
echo '</form>';
?>