Is it possible to have a form in a PHP file that processes itself without redirecting to a new page?
Yes, it is possible to have a form in a PHP file that processes itself without redirecting to a new page by using the same PHP file to handle form submission. This can be achieved by checking if the form has been submitted, processing the form data, and displaying the results on the same page. This can be done by using conditional statements to differentiate between the initial page load and form submission.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Form submitted, process the data
$name = $_POST['name'];
$email = $_POST['email'];
// Display the results
echo "Hello, $name! Your email is $email.";
} else {
// Display the form
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Submit">
</form>
<?php
}
?>