I am new to PHP and trying to understand how form validation is done. I am able to validate form fields and display error message as per comments in this post.
However the header function redirects the page but not posting the form fields to next page. How can post validated form values to next page?
Here is my code:
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Learning</title>
</head>
<body>
<?PHP
$nameError = $emailError = "";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$valid = true;
if(empty($_POST['name'])){
$valid=false;
$nameError = "Name missing";
}
if(empty($_POST['email'])){
$valid=false;
$emailError = "Email missing";
}
if($valid){
header('location:Processor.php');
exit();
}
}
?>
<form action="" method="post">
<input type="text" name="name" size="30"/> <?PHP echo $nameError; ?> <br>
<input type="text" name="email" size="30"/> <?PHP echo $emailError; ?> <br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Processor.php
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
echo "<br>", "Name: ", $_POST['name'];
echo "<br>", "Email: ", $_POST['email'];
?>
</body>
</html>