PHP HTML Contact Form


PHP HTML Contact Form

我在一个网站上有一个联系表格,它使用php脚本将详细信息发送到我的电子邮件。

我遇到的问题是"名字"字段中的数据根本没有发送到我的电子邮件。

我有一种感觉,这将是一个非常明显的错误,但我似乎找不到错误!

.PHP

<?php 
    $ToEmail = 'myemail@mycompany.com'; 
    $EmailSubject = 'BGM Contact Form'; 
    $mailheader = "From: ".$_POST["email"]."'r'n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."'r'n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1'r'n"; 
    $MESSAGE_BODY = "First Name: ".$_POST["firstName"].""; 
    $MESSAGE_BODY = "Last Name: ".$_POST["lastName"].""; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"]).""; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>

.HTML

<form action="thankyou.php"  onsubmit="return validateForm();" method="post">
    <div class="row collapse">
        <div class="large-2 columns">
            <label class="inline">First Name</label>
        </div>
        <div class="large-10 columns">
            <input type="text" id="firstName" name="firstName" placeholder="Jane" required>
        </div>
    </div>
    <div class="row collapse">
        <div class="large-2 columns">
            <label class="inline">Last Name</label>
        </div>
        <div class="large-10 columns">
            <input type="text" id="lastName" name="lastName" placeholder="Smith" required>
        </div>
    </div>
    <div class="row collapse">
        <div class="large-2 columns">
            <label class="inline" > Your Email</label>
        </div>
        <div class="large-10 columns">
            <input type="text" name="email" id="yourEmail" placeholder="jane@smithco.com" required>
        </div>
    </div>
    <label>Your Message</label>
    <textarea rows="8" name="message" required></textarea>
    <br><br>
    <button type="submit" class="radius button">Submit</button>
</form>

错误在这里

$MESSAGE_BODY = "First Name: ".$_POST["firstName"].""; 
$MESSAGE_BODY = "Last Name: ".$_POST["lastName"]."";

将其更改为

$MESSAGE_BODY = "First Name: ".$_POST["firstName"].""; 
$MESSAGE_BODY .= "Last Name: ".$_POST["lastName"].""; 

您正在覆盖包含名字的第一个 $MESSAGE_BODY

尝试以下操作,将".替换为单引号"

<?php 
    $MESSAGE_BODY .= "First Name: '$_POST["firstName"]'"; 
    $MESSAGE_BODY .= "Last Name: '$_POST["lastName"]'"; 
    $MESSAGE_BODY .= "Email: '$_POST["email"]'"; 
    $MESSAGE_BODY .= "Comment: 'nl2br($_POST["message"])'"; 
?>

php 脚本中存在问题

$ToEmail = 'myemail@mycompany.com'; 
        $EmailSubject = 'BGM Contact Form'; 
        $mailheader = "From: ".$_POST["email"]."'r'n"; 
        $mailheader .= "Reply-To: ".$_POST["email"]."'r'n"; 
        $mailheader .= "Content-type: text/html; charset=iso-8859-1'r'n"; 
        $MESSAGE_BODY = "First Name: ".$_POST["firstName"].""; 
        $MESSAGE_BODY .= "Last Name: ".$_POST["lastName"].""; 
        $MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
        $MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"]).""; 
        mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");

使用上面的 PHP 脚本