如何在php中自我验证后获取数据

时间:2016-04-07 02:12:00

标签: php

如何在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:123@hotmail.com"/>
        <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>

这是我的php表单... 它可以在同一页面验证自己,但无法将数据传递到另一个php页面.... 这是我的另一个PHP代码......

<?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";

?>

1 个答案:

答案 0 :(得分:1)

您可以使用$ _SESSION变量。

PHP $_SESSIONS

PHP Sessions and Cookies

因此,在验证用户设置$_SESSION['surname'] = $surname;

之后

然后在每个页面的顶部将session_start();添加到顶部。

然后在那个添加

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

查看PHP文档以获得更透彻的理解。

如果您希望用户能够创建帐户,您可能还需要考虑设置MYSQL数据库。

修改:表单页

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