简体   繁体   English

php isset()函数无法进行表单验证

[英]php isset() function not working on form validation

I have the following validation in place that I am confused at the way it works. 我已经进行了以下验证,但对它的工作方式感到困惑。 I am doing the standard isset() check on each of my fields and I want to print out an error message for each field that is not set. 我正在对每个字段进行标准的isset()检查,我想为未设置的每个字段打印一条错误消息。 However for some reason it's not behaving at all for me. 但是由于某种原因,它对我来说根本不起作用。

<?php 

  if (isset($_POST["submit"])) {

    //show hr on click
    $error_design = "<hr> <p id='error_msg'>Please Fix Below Errors: <p>";


    $user_name = $_POST["name"];
    $user_email = $_POST["email"];
    $user_age = $_POST["age"];

    $validation_errors = " ";
    $error_counter = null;

    if (isset($_POST["name"])) {
      $validation_errors .= "<li class='validation_errors'>Please input your name.</li>";
      $error_counter++;
    }

    if (isset($user_email)) {
      $validation_errors .= "<li class='validation_errors'>Please input your email.</li>";
      $error_counter++;
    }

    if (isset($user_age)) {
      $validation_errors .= "<li class='validation_errors'>Please input your age.</li>";
      $error_counter++;
    } elseif (intval($user_age) < 18) {
      $validation_errors .= "<li class='validation_errors'>You must be over 18 years old.</li>";
      $error_counter++;
    }


    if ($error_counter = null) {

      $validation_errors = "<li class='validation_success'>Message successfully sent!</li>";

    }

  }

?>


<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">

  <title>SandBox</title>

  <link rel="stylesheet" type="text/css" href="css/style.css">
  <link rel="stylesheet" href="css/animate.css">

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/script.js"></script>
</head>
<body>

  <header>

  </header>

  <form action="" method="post">

    <div class="input_group">
      <label for="name">Name</label>
      <input type="text" name="name">
    </div>

    <div class="input_group">
      <label for="email">eMail</label>
      <input type="email" name="email">
    </div>

    <div class="input_group">
      <label for="age">Age</label>
      <input type="text" name="age">
    </div>

    <input type="submit" value="Send" name="submit" id="submit">

    <?php echo $error_design; ?>

    <div>

      <ul>

        <?php echo $validation_errors; ?>

      </ul>

    </div>

  </form>


  <footer></footer>

</body>
</html>

That's because you are using wrong function here. 那是因为您在这里使用了错误的功能。

isset() checks if variable is set and is not null but when you send form with empty field you'll get empty string. isset()检查变量是否被设置并且不为null但是当您发送带有空字段的表单时,您将得到空字符串。 And isset() will return true in such case. 在这种情况下, isset()将返回true

You have to use empty() and just check string length to make sure that something is there. 您必须使用empty()并仅检查字符串长度以确保存在某些内容。

在您的情况下,您尝试检查变量是否未设置,但是在代码中实际检查变量是否已设置,因此将每个isset()替换为if(!isset())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM