HTML Post动作将我发送到php表单

时间:2017-04-23 03:38:20

标签: php html forms email contact

我是PHP的新手,我只想要一个简单的电子邮件联系表单,如果没有输入必填字段会显示错误,如果发送电子邮件,将会给出一条感谢信息。

当我点击我的提交按钮时,电子邮件会被发送,但网站会直接重定向到/send-email.php页面,该页面空白无用。如果表单未填写,所需的表单字段似乎也无法阻止发送电子邮件。

HTML表单代码:

            <form class="contact-form" action="php/send-email.php" method="post" novalidate>
                <div class="row">
                    <div class="column width-6 pad-1 contact-column">
                        <h5>Your Name*</h5>
                        <input type="text" name="name" class="form-box" tabindex="1" required>
                    </div>
                    <div class="column width-6 pad-1 contact-column">
                        <h5>Email*</h5>
                        <input type="text" name="email" class="form-box" tabindex="2" required>
                    </div>
                </div>
                <div class="row mt-2">
                    <div class="column width-6 pad-1 contact-column">
                        <h5>Your Website</h5>
                        <input type="text" name="website" class="form-box" tabindex="3">
                    </div>
                    <div class="column width-6 pad-1 contact-column">
                        <h5>Company</h5>
                        <input type="text" name="company" class="form-box" tabindex="4">
                    </div>
                </div>
                <div class="column width-12">
                    <input type="text" name="honeypot" class="form-honeypot">
                </div>
                <div class="row mt-2">
                    <div class="column width-12 pad-1">
                        <h5>Your Message*</h5>
                        <textarea name="message" class="form-text" tabindex="5" required></textarea>

                    </div>
                </div>
                <div class="row mt-2">
                    <input type="submit" value="Send Your Message!" class="btn btn-large bg-blue">

                </div>
            </form>

Php文件

<?php 

$recipient = "contact@mysite.com";

$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();

$subject = "New email from contact form";

$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Website: $website\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n\n\n";

if(mail($recipient, $subject, $email_content, $headers)){
echo "Thanks for the email, we'll get back to as soon as possible!";
}
?>

对正确方向的任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

检查空输入。尝试使用if else语句。例如

if($name == '')
{
    echo "Please fill in name";
}

else if($var == '')
{
    //error message
}

或者你可以尝试

if($name == '' || $email == '' || $var == '')
{
    echo "Please fill in all the blank";
}

我对PHP也很新,所以这只是一个简单的检查。希望它会有所帮助!