简体   繁体   English

我的联系表格不会验证其输入字段。 我的输入字段为空白时,它将向我发送电子邮件。 有什么建议么?

[英]My contact form will not validate its input fields. It will submit an email to me while my input fields are blank. Any suggestions?

As stated, my php contact form will send me a blank email message however, it will not validate null input values. 如前所述,我的php联系人表单将向我发送空白电子邮件,但是它不会验证空输入值。 Any suggestions? 有什么建议么? I've tried about everything I know to do. 我已经尝试了所有我想做的事情。

I'm also wondering if there is a way that I can redirect to a thankyou.html page on submit. 我也想知道是否有一种方法可以在提交时重定向到thankyou.html页面。 I remember the php being something like "header..." to redirect. 我记得php被重定向为“ header ...”。

 if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $from = 'Demo Contact Form'; 
    $to = 'email@example.com'; 
    $subject = 'Message from Contact Demo ';

    $body = "From: $name\n E-Mail: $email";

    // Check if name has been entered
    if (!$_POST['name']) = 0; {
        $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';
    }

 // If there are no errors, send the email
 if (!$errName && !$errEmail) {
    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>';
    }
   }
 }
  ?>

HTML... HTML ...

 <!--FORM-->
 <form role="form" action="contact.php" method="post" class="form-horizontal align-center">
 <div class="form-group col-centered">
  <label class="control-label col-sm-4 text-align-center">NAME</label>
    <div class="control col-sm-4">
        <input type="text" id="name" name="name" class="form-control">
          <?php echo "<p class='text-danger'>$errName</p>";?> 
    </div>
  </div>

 <div class="form-group col-centered">
  <label class="control-label col-sm-4 text-align-center">EMAIL</label>
    <div class="control col-sm-4">
        <input type="text" id="email" name="name" class="form-control">
          <?php echo "<p class='text-danger'>$errEmail</p>";?> 
    </div>
 </div>

 <div class="form-group">
  <label class="control-label  col-sm-4 text-align-center">PHONE</label>
    <div class="col-sm-4">
        <input type="text" name="phone" class="form-control">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
        <button type="submit" name="submit" value="submit" class="btnSize btn btn-default btn-lg"><strong>SAVE $150</strong></button>
    </div>
  </div>
 </form>

Thanks for your time. 谢谢你的时间。

Here's my take on it: 这是我的看法:

if (isset($_POST["submit"])) {
    $name = !empty( $_POST['name'] ) ? $_POST['name'] : false;
    $email = !empty( $_POST['email'] ) ? filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) : false;

    if( $name && $email ){

        $from = 'Demo Contact Form';
        $to = 'email@example.com';
        $subject = 'Message from Contact Demo ';

        $body = "From: $name\n E-Mail: $email";

        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{
        $errors = [];
        if( !$name ) $errors [] ='Invalid Name entered';

        if( !$email ) $errors [] ='Invalid Email entered';

        $result= '<div class="alert alert-danger">'.implode('<br>', $errors ).'</div>';
    }
}

filter_var() returns the filtered var or false on failure, so with that and the ternary {if} ? true : false; filter_var()返回过滤后的var或false,如果失败,那么返回三元{if} ? true : false; {if} ? true : false; we can normalize your input and simply everything after that. 我们可以将您的输入标准化,然后再对其进行标准化。

We check the inputs as soon as possible, that way we run as little code as needed to get to the end result. 我们会尽快检查输入,这样我们就可以运行尽可能少的代码来获得最终结果。 We know right away if they are good and can skip the majority of the email code, and output our error message. 我们立即知道它们是否不错,并且可以跳过大多数电子邮件代码,并输出我们的错误消息。

Logically we can dump the error flag, an in the else we know one or both inputs are false. 从逻辑上讲,我们可以转储错误标志,否则可以转储一个或两个输入为假的错误标志。 The array and implode is just a simpler way of putting in a line break. 数组和爆破只是换行的一种简单方法。 It's also easier to extend by just adding another input in with minimal code changes. 只需添加最少的代码更改即可添加另一个输入,因此扩展起来也更加容易。 As you can see it would be very easy to add another ternary statement, an and condition and an error message. 如您所见,添加另一个三元语句,and条件和错误消息将非常容易。 ( only 2.5 ish lines ) (仅2.5 ish线)

One thing I would do on top of that is this 在此之上我要做的一件事是

 $post = array_map('trim', $_POST);

And then change all the $_POST to $post , this will account for names added in as spaces or extra white space from Copy and Pasting, a common occurrence. 然后将所有$_POST更改为$post ,这将解决在复制和粘贴(常见的情况)中添加为空格或多余空白的名称。

Further you may be able to minimize the errors like this 此外,您也许可以最大程度地减少此类错误

}else{
    $result= '<div class="alert alert-danger">'.implode('<br>', array_filter([
        $name ? false : 'Invalid Name entered',
        $email ? false : 'Invalid Email entered',
    ])).'</div>;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM