How can PHP be used to check if a form has been submitted and display data accordingly, even when the page is initially loaded?
To check if a form has been submitted in PHP and display data accordingly, you can use the `$_SERVER['REQUEST_METHOD']` variable to determine if the form has been submitted using the POST method. You can then use an `if` statement to check if the form has been submitted and display the form data.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// Form has been submitted
$name = $_POST['name'];
$email = $_POST['email'];
echo "Form submitted with data:<br>";
echo "Name: $name<br>";
echo "Email: $email";
} else {
// Form has not been submitted, display form
echo "<form method='post'>";
echo "<input type='text' name='name' placeholder='Name'>";
echo "<input type='email' name='email' placeholder='Email'>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
}
?>