Chi Shen
Chi Shen

Reputation: 207

how to get data after self validation in php

how to pass the collected input to another page after self validate in php

<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>

<?php 

//define variable and set to empty value

$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr =$mobileTelNoErr = $sendMethodErr = "";
$forename = $surname = $email =  $postalAddress = $landLineTelNo = $mobileTelNo = $sendMethod = "";

if($_SERVER["REQUEST_METHOD"] =="POST"){
    $valid = true;


    if(empty($_POST["forename"])){
        $forenameErr = "Forename is required";
         $valid = false; //false
    } else {
        $forename = test_input($_POST["forename"]);
        // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$forename)) {
       $forenameErr = "Only letters and white space allowed";

    }
    }

    if(empty($_POST["surname"])){
        $surnameErr = "Surname is required";
         $valid = false; //false
    } else {
        $surname = test_input($_POST["surname"]);
        // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
       $surnameErr = "Only letters and white space allowed";
    }
    }   

    if(empty($_POST["postalAddress"])){
        $postalAddressErr =" Please enter postal address";
         $valid = false; //false
    } else {
         $postalAddress = test_input($_POST["postalAddress"]);
         }

    if(empty($_POST["landLineTelNo"])){
        $landLineTelNoErr = "Please enter a telephone number";
         $valid = false; //false
    } else {
        $landLineTelNo = test_input($_POST["landLineTelNo"]);
        // check if invalid telephone number added
        if (!preg_match("/^[0-9 ]{7,}$/",$landLineTelNo)) {
            $landLineTelNoErr = "Invalid telephone number entered";
        }
    }

    if(empty($_POST["mobileTelNo"])){
        $mobileTelNoErr = "Please enter a telephone number";
         $valid = false; //false
    } else {
        $mobileTelNo = test_input($_POST["mobileTelNo"]);
        // check if invalid telephone number added
        if (!preg_match("/^[0-9 ]{7,}$/",$mobileTelNo)) {
            $mobileTelNoErr = "Invalid telephone number entered";
        }
    }

    if(empty($_POST["email"])){
      $emailErr = "Email is required";  
         $valid = false; //false
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format";
         }
    } 

    if(empty($_POST["sendMethod"])){
        $sendMethodErr = "Contact method is required";
         $valid = false; //false
    } else {
        $sendMethod = test_input($_POST["sendMethod"]);
    }

 //if valid then redirect
    if($valid){
   header('Location: userdetail.php');
   exit();
}   
}

//check

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

?>

<div id="wrapper">

<h1>Welcome to Chollerton Tearoom! </h1>

<nav> 
    <ul>
         <li><a href="index.html">Home</a></li>
         <li><a href="findoutmore.html">Find out more</a></li>
         <li><a href="offer.html">Offer</a></li>
         <li><a href="credit.html">Credit</a></li>
         <li><a href="#">Admin</a></li>
         <li><a href="wireframe.html">WireFrame</a></li>
    </ul>
</nav>

<form id = "userdetail" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">

    <fieldset id="aboutyou">
    <legend id="legendauto">user information</legend>

        <p>
        <label for="forename">Forename: </label>
        <input type="text" name="forename" id="forename" value="<?php echo $forename;?>">
        <span class="error">* <?php echo $forenameErr;?></span>
        </p>

        <p>
        <label for="surname">Surname:</label>
        <input type="text" name="surname" id="surname" value="<?php echo $surname;?>">
        <span class="error">* <?php echo $surnameErr;?></span>
        </p>

        <p>
        <label for="postalAddress">Postal Address:</label>
        <input type="text" name="postalAddress" id="postalAddress" value="<?php echo $postalAddress;?>">
        <span class="error"> </span>
        </p>

        <p>
        <label for="landLineTelNo">Landline Telephone Number:</label>
        <input type="text" name="landLineTelNo" id="landLineTelNo" value="<?php echo $landLineTelNo;?>" >
        <span class="error"> * <?php echo $landLineTelNoErr;?></span>
        </p>

        <p>
        <label for="mobileTelNo">Moblie:</label>
        <input type="text" name="mobileTelNo" id="mobileTelNo" placeholder="example:012-3456789" value="<?php echo $mobileTelNo;?>" />
        <span class="error"><?php echo $mobileTelNoErr;?></span>
        </p>

        <p>
        <label for="email">E-mail:</label>
        <input type="text" name="email" id="email" value="<?php echo $email;?>" placeholder="example:[email protected]"/>
        <span class="error"> </span>
        </p>

        <fieldset id="future">
        <legend>Lastest news</legend>

        <p>
        Choose the method you recommanded to recevive the lastest information
        </p>
        <br>
        <input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="email") echo "checked";?>  value="email">
        Email
        <input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="post") echo "checked";?>  value="post">
        Post
        <input type="radio" name="sendMethod" id="sendMethod" <?php if (isset($sendMethod) && $sendMethod=="SMS") echo "checked";?>  value="SMS">
        SMS
        <span class="error">* <?php echo $sendMethodErr;?></span>
        </fieldset>

       <p><span class="error">* required field.</span></p>    

        <input type="checkbox" name="checkbox" value="check" id="agree" /> 
        I have read and agree to the Terms and Conditions and Privacy Policy

        <p> 
        <input type="submit" name="submit" value="submit" />
        </p>

        </form>

       </fieldset>
    </form>

</div>



</body>
</html>

here is my php form... it can validate itself in the same page but couldn't pass the data to another php page.... here is my another php code...

<?php 

$forenameErr = $surnameErr = $emailErr = $postalAddressErr = $landLineTelNoErr =$mobileTelNoErr = $sendMethodErr = "";
$forename = $surname = $email =  $postalAddress = $landLineTelNo = $mobileTelNo = $sendMethod = "";


echo "<h1>Successfull submission :</h1>";
echo "<p>Forename : $forename <p/>";
echo "<p>Surname : $surname <p/>";
echo "<p>Email: $email</p>";
echo "<p>Post Address: $postalAddress</p>";
echo "<p>Landline: $landLineTelNo</p>";
echo "<p>Mobile : $mobileTelNo</p>";
echo "<p>Contact method: $sendMethod";

?>

Upvotes: 0

Views: 46

Answers (1)

Kevin S
Kevin S

Reputation: 173

You can use $_SESSION variables.

PHP $_SESSIONS

PHP Sessions and Cookies

So after the users has been validated set $_SESSION['surname'] = $surname;

Then on the top of each page add session_start(); to the top.

Then Under that add

  if (isset($_SESSION['surname'])) {
      $surname = $_SESSION['surname'];
  } else {
      die();
  }

View the PHP docs for a more thorough understanding.

You may also want to look into setting up a MYSQL database if you want users to be able to create accounts.

Edit: form page

if($valid){
   $_SESSION['surname'] = $surname;
   $_SESSION['postalAddress'] = $postalAddress;
   header('Location: userdetail.php');
   exit();
}   

Upvotes: 1

Related Questions