2

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. 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>
1
  • look at my answer here for example of nifty validation class Commented Sep 13, 2015 at 1:04

2 Answers 2

4

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. And isset() will return true in such case.

You have to use empty() and just check string length to make sure that something is there.

Sign up to request clarification or add additional context in comments.

Comments

0

in your case you're trying to check if the variable is NOT set but in your code you actually checking if the variable IS set so replace every isset() with if(!isset())

4 Comments

That's not true. isset() also verifies if variable is not null, not only if it's declared.
i mean in his case he's trying to find out if the variable is not set but he's actually checking if the variable is set
Oh, you should definitely rewrite your answer to make it easier to understand ;)
sorry, my first language isn't English :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.