php - 在 PHP 中发送空白数据的联系表单

标签 php html css

我最近为我的 friend 制作了一个网站,其中包含一个使用 PHP 的联系表格。当此人发送电子邮件时,数据不会通过电子邮件发送。

这是网站的代码

<article class="grid_8">
    <div class="indent-left">
        <h3 class="p1">Contact us</h3>
        <form id="contact-form" method="post" name="myemailform" action="contact_form.php">  
            <fieldset>
                <label><span class="text-form">Name:</span><input name="name" type="text" required /></label>
                <label><span class="text-form">Email:</span><input name="email" type="text" required /></label>                              
                <label><span class="text-form">Phone:</span><input name="phone" type="text" /></label>                              
                <div class="wrapper">
                    <div class="text-form">Message:</div>
                    <div class="extra-wrap">
                        <textarea name="message" required></textarea>
                        <div class="clear"></div>
                        <div class="buttons">
                            <input type="submit" value="Submit" class="button">
                        </div> 
                    </div>
                </div>                            
            </fieldset>                     
        </form>
    </div>
</article>

这是 PHP 的代码

<?php

$name = $_POST['name'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$email_from = $_POST['email'];
$email_subject = $_POST["New Message From www.chevroncreativesolutions.com"];
$email_body = $_POST["You have received a new message from $name.\n".
                     "Here is the message:\n $message.\n You Can call me on $phone.\n"].

$to = "matthias.mccarthy@gmail.com";
$headers = "From: $email_from \r\n";
$contact_form=mail($to,$email_subject,$email_body,$headers);

if($contact_form) {
    echo "Message has been sent.";
    echo "<form><input type='button' value='Return back' onclick='history.back();'></form>";
} else {
    echo "Error In sending your message";
    echo "<form><input type='button' value='Return back' onclick='history.back();'></form>";
}

最佳答案

您正在将 $email_subject$email_body 值设置为未定义的 $_POST 值:

$email_subject = $_POST["New Message From www.chevroncreativesolutions.com"];
$email_body = $_POST["You have received a new message from $name.\n".
                     "Here is the message:\n $message.\n You Can call me on $phone.\n"];

相反,删除 $_POST[] 部分:

$email_subject = "New Message From www.chevroncreativesolutions.com";
$email_body = "You have received a new message from $name.\n".
              "Here is the message:\n $message.\n You Can call me on $phone.\n";

此外,但与您的问题无关,您需要清理 $email_from 字段以防止 header 注入(inject):

$email_from = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
if ($email_from === false) {
    echo 'error, invalid email';
}

关于php - 在 PHP 中发送空白数据的联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26126329/

相关文章:

javascript - 如何更改多个元素的样式?

html - 为所有子菜单添加固定位置和宽度

html - 隐藏选择列表箭头 CSS3

javascript - 无法制作隐藏/可见的错误消息

php - 如何检查数据库中是否存在用户名?

php - 需要解决IE 8 Jquery问题

javascript - 使用 javascript 或 jquery 突出显示域到期月份之前的行

html - 使用 :after and :before in IE7 的替代方法是什么

php - 通过 PHP 在 Wordpress 站点中动态包含 CSS

php - 允许在 PHP 中上传大文件(安全)