PHP Mail脚本无法获取表单数据

时间:2016-08-14 10:43:45

标签: php html forms email

尝试在网站上创建联系表单,表单代码如下所示:

1.txt

此表单应发送一封电子邮件,其中包含表单字段中的信息,但在用户按提交时无法从字段中获取数据。

这是send_email.php(替换了收件人电子邮件):

        <form name="contact_form" method="POST" action="send_email.php">
            <ul id="contact_form">
                <li>
                    <input required type="text" name="name" class="field_style field_split" placeholder="Your Name">
                    <input required type="email" name="email" class="field_style field_split" placeholder="Your Email">
                </li>
                <li>
                    <select required name="subject" class="field_style field_full">
                        <option value="General Enquiry">General Enquiry</option>
                        <option value="Coaching Enquiry">Coaching Enquiry</option>
                        <option value="Other">Other</option>
                    </select>
                </li>
                <li>
                    <textarea required name="message" class="field_style" placeholder="Message"></textarea>
                </li>
                <li>
                    <input id="submit_contact" name="submit" type="submit" value="Send Email">
                </li>
            </ul>
        </form>

它返回以下消息:

<?php
$to = "name@domain.com";

if(isset($_POST["from"])){ $from = $_POST["from"];}else{echo "from not set.";}
if(isset($_POST["name"])){ $name = $_POST["name"];}else{echo "name not set.";}
if(isset($_POST["subject"])){ $subject = $_POST["subject"];}else{echo "subject not set.";}
if(isset($_POST["message"])){ $message = $_POST["message"];}else{echo "message not set.";}

if(mail($to, $subject, $message))
{
    header("Location: #");
}else{
    echo "Error: ";
    print_r(error_get_last());
}
?>

任何人都有任何想法,因为我是PHP的新手并且不明白为什么不从表单中提取数据。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

从html发送POST变量:nameemailmessagesubject。但是在send_email.php文件中,您期望:fromnamesubjectmessage。如您所见,您尚未提供from

接下来,变量范围。无论$_POST中的参数是否已设置,$message$subject都超出范围且未在以下位置定义:

if(mail($to, $subject, $message))

以下应该工作:

的index.html:

<form name="contact_form" method="POST" action="send_email.php">
    <ul id="contact_form">
        <li>
            <input required type="text" name="name" class="field_style field_split" placeholder="Your Name">
            <input required type="email" name="from" class="field_style field_split" placeholder="Your Email">
        </li>
        <li>
            <select required name="subject" class="field_style field_full">
                <option value="General Enquiry">General Enquiry</option>
                <option value="Coaching Enquiry">Coaching Enquiry</option>
                <option value="Other">Other</option>
            </select>
        </li>
        <li>
            <textarea required name="message" class="field_style" placeholder="Message"></textarea>
        </li>
        <li>
            <input id="submit_contact" name="submit" type="submit" value="Send Email">
        </li>
    </ul>
</form>

send_email.php:

<?php
$to = "name@domain.com";
print_r($_POST);

$required_vars = array("from", "name", "subject", "message");
$flag = false;
foreach($required_vars as $req_v) {
    if (!isset($_POST[$req_v])) {
        echo "$req_v is not set!\n";
        $flag = true;
    }
}
if ($flag) {
    exit();
}
$ret = mail($to, $_POST["subject"], $_POST["message"]);
if ($ret) {
    print_r($ret);
    //header("Location: #");
} else {
    echo "Error: ";
    print_r(error_get_last());
}

?>

在此处阅读有关变量范围的更多信息:Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?