表单验证问题服务器端代码未验证

时间:2018-07-02 21:31:31

标签: php html bootstrap-4

我在验证表单时遇到问题。我按照建议遵循了w3schools指南,但无法使其与我的网站一起使用。我正在使用bootstrap 4,它似乎正在检查表单输入,以查看它们是否为空,但是就这样,所以我向php文件添加了一堆验证。但是,只要输入不为空,我就提交表单。因此,前端验证有效,而服务器端无效。这是我的代码。

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;
}
?>

0 个答案:

没有答案