使用php无法在xampp中进行表单验证

时间:2017-05-04 07:49:21

标签: php html html5

我无法使用php验证表单,即使未经验证,表单也会提交。我正在使用Xampp服务器。请让我知道解决方案。

<?php
  $error = "";

  if($_POST) {
      if(!$_POST["email"]) {
          $error .= "Email address is required<br>";
      }

      if(!$_POST["subject"]) {
          $error .= "Subject is required<br>";
      }

      if(!$_POST["exampleTextarea"]) {
          $error .= "Text Area address is required<br>";
      }

      if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
          $error .= "Email address is invalid<br>";
      }


      if($error != ""){
          $error = '<div class="alert alert-danger" role="alert"><p><strong>Oh snap! There were error(s) in your form:</strong></p>'. $error . '</div>';
      }
  }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
  </head>
  <body>
    <div class="container"id="containerID">
      <h1>Get in touch!!!</h1>
      <div id="error"><? echo $error; ?></div>
      <form method="post">
        <div class="form-group">
          <label for="email">Email address</label>
          <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" name="email">
          <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
          <label for="subject">Subject</label>
          <input type="text" class="form-control" id="subject" name="subject">
        </div>

        <div class="form-group">
          <label for="exampleTextarea">What would you like to ask us??</label>
          <textarea class="form-control" id="exampleTextarea" rows="5" name="exampleTextarea"></textarea>
        </div>

        <button type="submit" class="btn btn-primary" id="submit">Submit</button>
      </form>
      </div>
      <!--    jQuery first, then Tether, then Bootstrap JS. -->
      <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
      <script type="text/javascript">
      </script>    
    </body>
  </html>

3 个答案:

答案 0 :(得分:1)

您的代码无法正确处理表单的问题,您正在覆盖您正在检查的变量$error的值 if ($error != "") { $error = '<div class="alert alert-danger" role="alert"><p><strong>Oh snap! There were error(s) in your form:</strong></p>'. $error . '</div>'; }这将导致忽略您在此代码之上所做的所有先前错误分配。在页面上重新加载$error初始化"",因此验证错误和填充$error变量的所有艰苦工作都变得毫无用处。因此,我在答案中使用了会话。

<?php
$error = "";
$errors = "";
session_start();
if(isset($_POST['submit'])) {
    if(empty($_POST["email"])) {
        $error .= "Email address is required<br>";
    } else if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
        $error .= "Email address is invalid<br>";
    }
    if(empty($_POST["subject"])) {
        $error .= "Subject is required<br>";
    }

    if(empty($_POST["exampleTextarea"])) {
        $error .= "Text Area address is required<br>";
    }

    if($error != ""){
        $errors = '<div class="alert alert-danger" role="alert"><p><strong>Oh snap! There were error(s) in your form:</strong></p>'. $error . '</div>';
        $_SESSION['errors'] = $errors;
    }

}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
<div class="container"id="containerID">
    <h1>Get in touch!!!</h1>
    <?php
        if (isset($_SESSION['errors'])) {
            echo $_SESSION['errors'];
            unset($_SESSION['errors']);
        }
    ?>
    <form method="post">
        <div class="form-group">
            <label for="email">Email address</label>
            <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" name="email">
            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
        <div class="form-group">
            <label for="subject">Subject</label>
            <input type="text" class="form-control" id="subject" name="subject">
        </div>

        <div class="form-group">
            <label for="exampleTextarea">What would you like to ask us??</label>
            <textarea class="form-control" id="exampleTextarea" rows="5" name="exampleTextarea"></textarea>
        </div>

        <button type="submit" name="submit" class="btn btn-primary" id="submit">Submit</button>
    </form>
</div>
<!--    jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script type="text/javascript">
</script>
</body>
</html>

答案 1 :(得分:0)

尝试为<button>提供一个名称,例如<button name="submitbtn">,并在if语句的顶部,尝试if(isset($_POST['submitbtn'])

我不知道这会在您的代码中有效,但这正是我通常所做的。

答案 2 :(得分:0)

而不是

<div id="error"><? echo $error; ?></div>

试试这个

<div id="error">

<?php

 echo $error; 

?>

</div>