Tags
Asked 3 years ago
9 Jun 2021
Views 333
Mitul Dabhi

Mitul Dabhi posted

HTML form PHP post to self to validate or submit to new page

HTML form PHP post to self to validate or submit to new page
Phpworker

Phpworker
answered Apr 26 '23 00:00

In PHP, when a form is submitted, you can either validate the form data and show any errors on the same page, or you can submit the form to a new page for further processing. Here is an example of how you can achieve either option:

1.Validate the form data and show errors on the same page:


<?php
// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form data
    $name = $_POST["name"];
    $email = $_POST["email"];
    
    // Perform form validation
    $errors = array();
    if (empty($name)) {
        $errors[] = "Name is required";
    }
    if (empty($email)) {
        $errors[] = "Email is required";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Invalid email format";
    }
    
    // If there are no errors, process the form
    if (empty($errors)) {
        // Insert the form data into the database, send an email, etc.
        // ...
        // Redirect to a success page
        header("Location: success.php");
        exit();
    }
}

?>

<!DOCTYPE html>
<html>
<head>
	<title>Form Validation</title>
</head>
<body>
	<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
		<label for="name">Name:</label>
		<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>">
		<br>
		<label for="email">Email:</label>
		<input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>">
		<br>
		<input type="submit" value="Submit">
	</form>
	<?php if (!empty($errors)): ?>
		<ul>
			<?php foreach ($errors as $error): ?>
				<li><?php echo htmlspecialchars($error); ?></li>
			<?php endforeach; ?>
		</ul>
	<?php endif; ?>
</body>
</html>

In this example, we use htmlspecialchars () to prevent cross-site scripting (XSS) attacks by escaping any special characters in the user input. We then use $_SERVER["PHP_SELF"] as the form action to submit the form to the same page.

When the form is submitted, we get the form data and perform form validation. If there are any errors, we add them to an array and display them on the same page. If there are no errors, we process the form and redirect to a success page.

2.Submit the form to a new page for further processing:


<!DOCTYPE html>
<html>
<head>
	<title>Form Submission</title>
</head>
<body>
	<form method="post" action="submit.php">
		<label for="name">Name:</label>
		<input type="text" name="name">
		<br>
		<label for="email">Email:</label>
		<input type="text" name="email">
		<br>
		<input type="submit" value="Submit">
	</form>
</body>
</html>

In this example, we use action="submit.php" to submit the form to a new page (submit.php ) for further processing. In submit.php, we can use $ _POST to access the form data and perform any necessary processing.
Post Answer