PHP form validation and submitting another page

I am new to PHP and am trying to understand how form validation is done. I can validate the form fields and display the error as per the comments in this post .

However, the header function redirects the page but does not send the form fields to the next page. How do I submit valid form values ​​to the 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  Execution</title>
</head>
<body>
<?php

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

      

+3


source to share


5 answers


header('location:Processor.php');

sends browser to another page, but not POST values. Either execute functions on one page, or use sessions to transfer data to another page. The first option is recommended.



+1


source


The header function is used to redirect to another page. It will not pass the post variable. You can do this with either session variables or cookies. For validating the front end of a form, I suggest using javascript (jQuery if you're comfortable). Although use php for authentication only.

If you need 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  Execution</title>
</head>
<body>
<?php

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

      



To test the interface with 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");
    }
});

      

+2


source


This is a relatively old post, but I often use a solution that is not listed here, so I thought I'd share in case others find it 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 your html form header, just add

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

      

+2


source


if you are using the header ('location: Processor.php'); the varayable session does not go to the Processor.php page, so you must use include ('Processor.php');

0


source


include should only do fine.

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

      

-1


source







All Articles