PHP form validation and submit to another page-ThrowExceptions

Exception or error:

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>
How to solve:

header('location:Processor.php'); sends the browser to another page, but not the POST values. Either do the functions in the same page, or use Sessions to pass data to another page. First option is recommended.

Answer:

header function is used to redirect to a different page. It wont pass post variable. You can do it either by using session variables or by using cookies.
For form front end validation I suggest the use of javascript (jQuery if you are comfortable). While use php only for backend validation.

If you have to use php for front end validation, use session

php
session_start();
/* ---- Your other validation code -- */
if($valid){
$_SESSION['email'] = $_POST['email'];
$_SESSION['name'] = $_POST['name'];
 header('location:Processor.php');
 exit();
}


processor page : 
 <html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php

   echo "<br>", "Name: ", $_SESSION['name'];
   echo "<br>", "Email: ", $_SESSION['email'];
?>
</body>
</html>

For frontend validation using js

 <form action="Processor.php" method="post">
  <input type="text" name="name" id="name" size="30"/> <?PHP echo $nameError; ?> <br>
  <input type="text" name="email" id="email" size="30"/> <?PHP echo $emailError; ?> <br>
  <input type="submit" value="Submit" id="submit"/>
</form>

JS:
$("#submit").click(function(){
   var name = $("#name").val();
   var email = $("#email").val();
   if(name="" || email=""){
     event.preventDefault();
     alert("Please fill all the feilds");
    }
});

Answer:

This is a relatively old post but I use a solution rather frequently that is not listed here so I thought I would share in case others find this handy.

<?PHP

if($_SERVER['REQUEST_METHOD'] == "POST"){
    if(empty($_POST['name'])){
        header('Location: back_to_your_form.php?error=Name+missing');
        exit();
    }

    else if(empty($_POST['email'])){
        header('Location: back_to_your_form.php?error=Email+missing');
        exit();

    } else {
    // perform a function of some sort
    }
}    

?>

And then in the header of the html form simply add

<? if (isset($_GET['error'])) { ?>
    <script>alert('<? echo $_GET['error']; ?>');</script> 
<? } ?>

Answer:

if you use the header(‘location:Processor.php’); the session varaiable don’t pass to the page Processor.php, so you have to use include(‘Processor.php’);

Answer:

include should do just fine.

if($valid){
    include('Processor.php');
    exit();
}

Leave a Reply

Your email address will not be published. Required fields are marked *