PHP数据验证提交不良数据

时间:2018-10-11 07:52:05

标签: php html validation

我目前使我的代码工作到某种状态。

当用户输入数据名称,电子邮件和公司时,他们将提交表单,它将回显输入,这很好,但是当我在表单中输入无效数据并提交时,仍会张贴但显示else语句。

我是否错过了Preg_match中的某些内容,或者这只是编写验证代码的一种不好方法?

 <!DOCTYPE html>
    <html>
    <head>
      <title>Visitor Sign in</title>
      <meta charset="utf-8" />
      <link rel="stylesheet" type="text/css" href="visitor.css"/>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>

    </head>

    <body>

    <div id="wrapper">
        <img src="Wincanton.png" alt="wincantonLogo" class="wincantonLogo" />
        <img src="Screwfix.png" alt="screwfixLogo" class="screwfixLogo" />
        <div style="clear:both"></div><br>

    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $companyErr = "";
    $fullname = $email = $company = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["fullname"])) {
        $nameErr = "Name is required";
      } else {
        $fullname = test_input($_POST["fullname"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$fullname)) {
          $nameErr = "Only letters and white space allowed"; 
        }
      }

      if (empty($_POST["email"])) {
        $emailErr = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/",$email)) {
          $emailErr = "Invalid email format";
        }
      }

      if (empty($_POST["company"])) {
        $companyErr = "Name is required";
      } else {
        $company = test_input($_POST["company"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$company)) {
          $companyErr = "Only letters and white space allowed"; 
        }
      }
    }
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }

    ?>


    <h1>Visitor Sign in</h1><br>

    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
      Name: <input type="text" name="fullname" >
      <span class="error">* <?php echo $nameErr;?></span>
      <br><br>
      E-mail: <input type="text" name="email">
      <span class="error">* <?php echo $emailErr;?></span>
      <br><br>
      Company: <input type="text" name="company">
      <span class="error"><?php echo $companyErr;?></span>
      <br><br>
      <br><br>
      <input type="submit" name="submit" value="Submit">  
    </form>


    <?php

    echo "<h2>Your Input:</h2>";
    echo $fullname;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $company;
    echo "<br>";

    ?>


    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

尝试isset条件。

if(isset($_POST['submit'])){
}