如何解决解析错误:语法错误,文件意外结束

时间:2020-04-22 10:43:25

标签: php html forms validation

目前我正在做一个关于图书馆管理系统的项目。为此,我正在使用php和html语言。 我正在尝试应用表单验证,但出现此错误。我是php的初学者,因此需要一些帮助。我彻底检查了代码,但仍然遇到此错误。

HTML代码



    <?php
    require '..\\model\signup.php';
    require 'adu.php'
    ?>
   
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
    <head>
    <meta charset="utf-8">
    
    <style media="screen">
    
    main{
      margin-left:300px;
      margin-top:70px;
    }
    </style>

    <title>Create Librarian</title>
   </head>
   <body>
   <?php include '..\\templates\dashboard.php'; ?>
   <main>
    <h3 class="my-4">Create new librarian</h3>
    <p><span class="error">* required field</span></p>
    <form  action="adu.php" method="post" name="">
      <div class="form-group" >

       
       <label class="mt-2" for="name">Name</label>
       <input id="name" class="form-control" type="text" name="name" value="<?php echo $fname;?>">
       <span class="error">* <?php echo $fnameErr;?></span>
       <br><br>
      
      
       <label class="mt-2" for="surname">Surname</label>
       <input class="form-control" type="text" name="surname" value="<?php echo $fname;?>">
       <span class="error">* <?php echo $lnameErr;?></span>
       <br><br> 

       <label class="mt-2" for="email">Email</label>
       <input class="form-control" type="text" name="email" value="<?php echo $email;?>">
       <span class="error">* <?php echo $emailErr;?></span>
       <br><br>

       
       <label class="mt-2" for="password">Password</label>
       <input class="form-control" type="password" name="password" value="<?php echo $pass;?>">
       <span class="error">* <?php echo $passErr;?></span>
       <br><br>      

   
       <label class="mt-2" for="phone">Phone</label>
       <input class="form-control" type="text" name="phone" value="<?php echo $phone;?>">
       <span class="error">* <?php echo $phoneErr;?></span>
       <br><br>   




       <input type="submit" name="submit" value="Create" class="mt-2 btn btn-primary">
       
      </div>


      </form>

      </main>


    </body>
    </html>



php代码


#**php code**#
    <?php
    // define variables and set to empty values
    $fnameErr = $lnameErr= $emailErr = $passErr = $phoneErr = "";
    $fname = $email = $lname = $pass = $phone = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
    $fnameErr = "First Name is required";
    } else {
    $fname = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $fnameErr = "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 (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
     }
   }
    
    if (empty($_POST["surname"])) {
    $lnameErr = "Surname is required";
    } else {
    $lname = test_input($_POST["surname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lnameErr = "Only letters and white space allowed";
      }
    }

   if (empty($_POST["password"])) {
    $passErr = "Password is required";
   } else {
    $pass = test_input($_POST["password"]);
    // check if name only contains letters and whitespace
    $uppercase = preg_match('@[A-Z]@', $password);
    $lowercase = preg_match('@[a-z]@', $password);
    $number    = preg_match('@[0-9]@', $password);
    $specialChars = preg_match('@[^\w]@', $password);
    if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8) {
    echo 'Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.';

      }
    }

    if (empty($_POST["phone"])) {
    $phoneErr = "phone number is required";
    } else {
    $phone = test_input($_POST["phone"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[0-9]{5}-[0-9]{5}$/",$phone)) {
      $phoneErr = "phone number should be numeric ";
      }
    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    echo "<h2>Your Input:</h2>";
    echo $fname;
    echo "<br>";
    echo $lname;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $pass;
    echo "<br>";
    echo $phone;
    echo "<br>";
    ?>

我该怎么办?

0 个答案:

没有答案