Form validation issues serverside code is not vali

2019-08-21 13:03发布

问题:

Im having issues with validating my form. I followed the w3schools guide as suggested and I cant get it to work with my site. Im using bootstrap 4 which seems to check the forms inputs to see if they are empty but thats it so I added a bunch of validation to the php file. Yet when I submit as long as the inputs are not empty the form is submitted. So front side validation is working but serverside is not. here is my code.

html:

 <div class="container">
        <div class="col-sm-8 col-sm-push-2">
        <form class="form inline d-flex justify-content-center" action="contact.php" method="POST" role="form">
             <br style="clear:both">
            <div class="col-sm-6">
              <div class="form-group">
                <input type="text" class="control" id="first_name" name="first_name" placeholder="First Name" required>
                <span class="error">* <?php echo $nameErr;?></span>
              </div>
              <div class="form-group">
                <input type="text" class="control" id="email" name="email" placeholder="Email" required>
                <span class="error">* <?php echo $emailErr;?></span>
              </div>
            </div>
            <div class="col-sm-6">
              <div class="form-group">
                <input type="text" class="control" id="last_name" name="last_name" placeholder="Last Name" required>
                <span class="error">* <?php echo $nameErr;?></span>
              </div>
              <div class="form-group">
                <input type="text" class="control" id="phone" name="phone" placeholder="Phone" required>
                <span class="error">* <?php echo $phoneErr;?></span>
              </div>
            </div>
            <div class="col-sm-12">
              <div class="form-group">
                <textarea name="message" class="control" id="message" placeholder="What's on your mind?" rows="3"></textarea>
              </div>
              <button type="submit" id="submit" name="submit" class="btn btn-primary">SEND</button>
          </form>
          </div>
        </div>

php:

<?php

$nameErr = $emailErr = $phoneErr = "";
$first_name = $last_name = $email = $phone = $message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $first_name = $_POST['first_name'];
  $last_name  = $_POST['last_name'];
  $email      = $_POST['email'];
  $phone      = $_POST['phone'];
  $message    = $_POST['message'];

  if (empty($first_name)) {
    $nameErr = "First name is required";
  } else {
    $first_name = check_input($first_name);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed";
    }
  }

  if (empty($last_name)) {
    $nameErr = "Last name is required";
  } else {
    $last_name = check_input($last_name);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
      $nameErr = "Only letters and white space allowed";
    }
  }

  if (empty($email)) {
    $emailErr = "Email is required";
  } else {
    $email = check_input($email);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }

  if (empty($phone)) {
      $phoneErr = "Phone number is required";
  } else {
    $phone = check_input($phone);

    if (!preg_match("'^(([\+]([\d]{2,}))([0-9\.\-\/\s]{5,})|([0-9\.\-\/\s]{5,}))*$'",$phone)) {
        $phoneErr = "Invalid Phone Number";
    }
  }

  if (empty($message)) {
    $message = "";
  } else {
    $message = check_input($message);
  }


$email_from =' Client, llc';
$email_subject = 'New Message From A Guest';
$email_body = "Name: $first_name $last_name\n".
              "Email: $email\n".
              "Phone: $phone\n".
              "Message: $message.\n";
$to ="me@work.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";

mail($to,$email_subject,$email_body,$headers);
header("location: thanks.html");
}

function check_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>