PHP表单验证在提交时不起作用


PHP form validation not working on submit

我有一个链接到php文件的HTML表单。当我点击提交时,表格发送,我收到一封电子邮件。但是,如果表单为空,并且我单击提交,它仍然会重定向到主页。

表单HTML:

     <form class="form-horizontal" role="form" method="post" action="contact.php"> 
                <div class="form-group"> 
                    <label for="name" class="col-sm-2 control-label">Name</label> 
                    <div class="col-sm-10"> 
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name"> 
                                        </div> 
                </div> 
                <div class="form-group"> 
                    <label for="telephone" class="col-sm-2 control-label">Phone</label> 
                    <div class="col-sm-10"> 
                        <input type="telephone" class="form-control" id="telephone" name="telephone" placeholder="Phone Number" > 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label for="email" class="col-sm-2 control-label">Email</label> 
                    <div class="col-sm-10"> 
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" > 
                    </div> 
                </div> 
                <div class="form-group"> 
                  <label for="dropdown" class="col-sm-2 control-label">Select One:</label>
                  <div class="col-sm-10">
                  <select name="select" class="form-control" id="dropdown">
                    <option>Driver</option>
                    <option>Advertiser</option>
                    </select>
                </div>
                </div>
                <div class="form-group"> 
                    <label for="message" class="col-sm-2 control-label">Message</label> 
                    <div class="col-sm-10"> 
                        <textarea class="form-control" rows="4" name="message"></textarea> 
                    </div> 
                </div>
                <div class="form-group"> 
                    <div class="col-sm-10 col-sm-offset-2"> 
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary"> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <div class="col-sm-10 col-sm-offset-2"> 
                    </div> 
                </div>
            </form>           

contact.php

    <?php
    if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $telephone= $_POST['telephone'];
            $select = $_POST['select'];
    $message = $_POST['message'];
    $from = 'Contact Form'; 
    $to = 'myemail@gmail.com'; 
    $subject = 'Message from Contact Form';
    $body ="From: $name'n E-Mail: $email'n Telephone: $telephone'n Select: $select'n Message:'n $message";
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }
    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }
    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }
    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errTel) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
    {
    //  To redirect form on a particular page
    header("Location:http://www.mywebsite.co");
    }
    }
    ?>

你知道为什么我的验证不起作用吗?

谢谢。

您的代码中有两个额外的括号,这是:

 {
 //  To redirect form on a particular page
 header("Location:http://www.mywebsite.co");
 }

应该只是:

 //  To redirect form on a particular page
    header("Location:http://www.mywebsite.co");

你关闭了if块,打开了一个空块。

您可能需要重新检查此部分:

{
//  To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}

此外,您还应该对POST数据使用trim功能。

contact.php

if (isset($_POST["submit"])) {
  $name      = trim($_POST['name']);
  $email     = trim($_POST['email']);
  $telephone = trim($_POST['telephone']);

  /* Other code */
  if (!$errName && !$errEmail && !$errMessage && !$errTel) {
      if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
      } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
      }
  } else {
   //  To redirect form on a particular page
   header("Location:http://www.mywebsite.co");
  }