How can the PHP script be modified to display the input values and errors in a more user-friendly manner?
To display the input values and errors in a more user-friendly manner, you can use HTML and CSS to format the output. You can create a table to display the input values and use styling to highlight any errors. Additionally, you can provide clear messages to inform the user about any validation errors.
<?php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$errors = array();
// Validate name
if (empty($name)) {
$errors[] = "Name is required";
}
// Validate email
if (empty($email)) {
$errors[] = "Email is required";
}
// Display input values and errors
echo "<table>";
echo "<tr><td>Name:</td><td>$name</td></tr>";
echo "<tr><td>Email:</td><td>$email</td></tr>";
echo "</table>";
if (!empty($errors)) {
echo "<div style='color: red;'>";
foreach ($errors as $error) {
echo $error . "<br>";
}
echo "</div>";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>