联系表单在发送之前不会验证字段

时间:2015-12-30 11:36:41

标签: php forms validation contact-form

我不熟悉表格,但我使用的表格通过填写联系表格发送电子邮件。

如果未正确输入字段,则会显示错误消息但仍会发送电子邮件。

我认为我没有验证表格,也不知道这样做的代码。

我需要,如果出现错误,在正确填写该字段之前,它不会发送电子邮件。

谢谢!

PHP:

<?php
/*
* Contact Form Class
*/

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$to = 'example@example.com';
$message_min_length = 5; // Min Message Length
$message = "
    <html>
    <head>
    </head>
    <body>
    <table>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["name"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["event_date"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["guests"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["hear_about"]."</td></tr>
    <tr><td>Telephone:</td><td style=\"color: red;\"> ".$_POST["message"]."</td></tr>
     </table>
    </body>
    </html>
    ";

$headers .= "From: ".$_POST["name"]." <".$_POST["email"].">\r\n";
mail($to,$subject = 'Website Contact Form Submission',$message,$headers);



class Contact_Form{
    function __construct($details, $to, $message_min_length, $email_message){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Website Contact Form Submission'; // Subject
        $this->telephone = strip_tags($details['telephone']);
        $this->event_date = strip_tags($details['event_date']);
        $this->guests = strip_tags($details['guests']);
        $this->hear_about = strip_tags($details['hear_about']);
        $this->message = strip_tags($details['message']);

        $this->to = $to;
        $this->message_min_length = $message_min_length;

        $this->response_status = true;
        $this->response_html = '';

    }


    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }


    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = false;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = false;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid email address</p>';
            $this->response_status = false;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = false;
        }
    }

    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());

    if($mail)
        {
            $this->response_status = true;
            $this->response_html = '<p>Message Sent. Thank You!</p>';
        }
    }


    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}


$contact_form = new Contact_Form($_POST, $to, $message_min_length);
$contact_form->sendRequest();

?>

FORM:

<form name="contact-form" class="contact-form" method="POST" role="form" action="contact_form.php">

    <div class="form-input-group">
        <i class="fa fa-male"></i><input type="text" class="" name="name" required maxlength="80" placeholder="Full name" novalidate="novalidate">
    </div>

    <div class="form-input-group">
        <i class="fa fa-paper-plane"></i><input type="text" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">
    </div>

    <div class="form-input-group">
        <i class="fa fa-phone"></i><input type="text" name="telephone" required maxlength="15" placeholder="Phone number" novalidate="novalidate">
    </div>

    <div class="form-input-group">
        <i class="fa fa-calendar-o"></i><input type="text" name="event_date" required maxlength="30" placeholder="Event date" novalidate="novalidate">
    </div>

    <div class="form-input-group">
        <i class="fa fa-users"></i><input type="text" name="guests" required maxlength="30" placeholder="Guest amount" novalidate="novalidate">
    </div>

    <div class="form-input-group">
        <i class="fa fa-bullhorn"></i><input type="text" name="hear_about" required maxlength="80" placeholder="How did you hear about us?" novalidate="novalidate">
    </div>

    <div class="form-input-group text-container">
        <i class="fa fa-envelope"></i><textarea name="message" class="text" maxlength="1000" cols="25" rows="6" placeholder="Message" required novalidate="novalidate"></textarea>
    </div>

    <button type="submit" class="btn-fill form-btn" id="contact-submit">Submit</button>

    <div id="response"></div>

</form>

2 个答案:

答案 0 :(得分:0)

您需要在发送邮件之前进行服务器端验证。 例如。进行电子邮件验证以检查电子邮件的格式是否正确。 有几种方法可以做到这一点:

1:这可能会对你有所帮助     http://www.w3schools.com/php/php_form_validation.asp 2:你也可以改变这个

 <i class="fa fa-paper-plane"></i><input type="text" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">

 <i class="fa fa-paper-plane"></i><input type="email" name="email" required maxlength="30" placeholder="Email" novalidate="novalidate">

希望这有帮助。

答案 1 :(得分:0)

您已经拥有了必需的属性,并且您不再需要在php中使用验证功能。此外,如果您的浏览器支持html5,那么您可以更改此

<input type="text" name="email"

<input type="email" name="email" pattern="/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i">

然后它会照顾好自己。只是检查你是否有任何javascript错误?