php 联系表无效,出现错误消息并退回

php contact form is not working, error message & bouncing back

所以当涉及到 php 表单时我真的很糟糕,我收到消息“抱歉, 无法发送您的消息...请检查您的电子邮件是否正确,否则会丢失一个字段..."

其他人正在使用该网站,说它也在反弹。

HOW IT LOOKS

PHP

该文件夹不在 php 文件夹中,它在 html 页中。

<?php
//set your email here:
$yourEmail = 'emma@peacehavengc.com';
/*
 * CONTACT FORM
 */
//If the form is submitted
if(isset($_POST['submitted'])) { 
    //Check to make sure that the name field is not empty
    if($_POST['contact_name'] === '') { 
            $hasError = true;
    } else {
            $name = $_POST['contact_name'];
    }

    //Check to make sure sure that a valid email address is submitted
    if($_POST['emma@peacehavengc.com'] === '')  { 
            $hasError = true;
    } else if (!preg_match("/^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}$/i", $_POST['emma@peacehavengc.com'])) {
            $hasError = true;
    } else {
            $email = $_POST['emma@peacehavengc.com'];
    }

    //Check to make sure comments were entered  
    if($_POST['contact_textarea'] === '') {
            $hasError = true;
    } else {
            if(function_exists('stripslashes')) {
                    $comments = stripslashes($_POST['contact_textarea']);
            } else {
                    $comments = $_POST['contact_textarea'];
            }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {

            $emailTo = $yourEmail;
            $subject = "Message From Your Website";
            $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
            $headers = 'From : my site <'.$emailTo.'>' . "\r\n" . 'answer to : ' . $email;

            mail($emailTo, $subject, $body, $headers);

            $emailSent = true; 
    }

}
?>

HTML

<div class="center">
    <h3>Get in touch Now</h3>
</div>
<form class="form-horizontal" method="post" action="contact.php" id="form">
    <div class="form-group">
        <label for="contact_name" class="col-lg-2 control-label">Name</label>
        <div class="col-lg-10">
            <input type="text" class="form-control" id="contact_name" name="contact_name">
        </div>
    </div>
    <div class="form-group">
        <label for="contact_email" class="col-lg-2 control-label">Email</label>
        <div class="col-lg-10">
            <input type="email" class="form-control" id="contact_email" name="contact_email">
        </div>
    </div>
    <div class="form-group">
        <label for="contact_textarea" class="col-lg-2 control-label">Message</label>
        <div class="col-lg-10">
            <textarea class="form-control" rows="3" id="contact_textarea" name="contact_textarea"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <input type="hidden" name="submitted" id="submitted" value="true" />
            <button type="submit" class="btn btn-default" name="submitted"><i class="icon-paperplane"></i>Send</button>
        </div>
    </div>
</form>

检查这个。

<?php
    $yourEmail = 'emma@peacehavengc.com';
    if(isset($_POST['submitted'])) 
    { 
        if($_POST['contact_name'] == '') 
        { 
                $hasError = true;
            } 
        else 
        {
                $name = $_POST['contact_name'];
            }

            //Check to make sure sure that a valid email address is submitted
            if($_POST['contact_email'] == '')  
        { 
                $hasError = true;
            } 
        else if (!preg_match("/^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}$/i", $_POST['contact_email'])) 
        {
                $hasError = true;
            } 
        else 
        {
                $email = $_POST['contact_email'];
            }

            //Check to make sure comments were entered  
            if($_POST['contact_textarea'] == '') 
        {
                $hasError = true;
            } 
        else 
        {
                if(function_exists('stripslashes')) 
            {
                        $comments = stripslashes($_POST['contact_textarea']);
                    } 
            else 
            {
                        $comments = $_POST['contact_textarea'];
                    }
            }

            //If there is no error, send the email
            if(!isset($hasError)) 
        {

                $emailTo = $yourEmail;
                $subject = "Message From Your Website";
                $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";            
        $headers  = "From: my site <$emailTo>" . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";

                mail($emailTo, $subject, $body, $headers);

                $emailSent = true; 
            }
    header("location:contact.html");
    }
    ?>