0

I am not able to validate the form using php and form is getting submitted even though its not validated. I am using Xampp sever. Please let me know the solution.

<?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>
0

3 Answers 3

1

Problem with your code not handling form post correctly, your are overriding the value for variable $error where you are checking 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>'; } this will lead to neglect all the previous error assignment you have made above this piece of code. Plus on page reload the $error initializes with "", so all the hard work in validating errors and populating the $error variable becomes useless. Therefore, I have used session in my answer.

<?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>
Sign up to request clarification or add additional context in comments.

Comments

0

Try giving your <button> a name, like <button name="submitbtn"> and at the top of the if statement, try if(isset($_POST['submitbtn'])

I don't know will this work in your code, but this is what I normally do.

Comments

0

Instead of

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

try this

<div id="error">

<?php

 echo $error; 

?>

</div>

2 Comments

What would this change?
This won't make a difference. See my answer above

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.