名字和消息未显示在电子邮件正文中

时间:2014-02-08 15:07:02

标签: php html ajax post contact-form

我在下面创建了一个联系人表单,由于某种原因,当我提交条目时,名字显示在标题上但不显示消息正文。消息也一样。有人知道这个问题吗?

  PHP



<?php
$field_name = $_POST['first_name'];
$field_last = $_POST['last_name'];
$field_email = $_POST['cf_email'];
$field_city = $_POST['cf_city'];
$field_state = $_POST['cf_state'];
$field_postal = $_POST['cf_postal'];
$field_country = $_POST['cf_country'];
$field_profession = $_POST['cf_profession'];
$field_industry = $_POST['cf_industry'];
$field_job = $_POST['cf_job'];
$field_linkedin = $_POST['cf_linkedin'];
$field_facebook = $_POST['cf_facebook'];
$field_instagram = $_POST['cf_instagram'];
$field_message = $_POST['cf_message'];

$mail_to = 'membership@name.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'City: '.$field_city."\n";
$body_message .= 'State/Province: '.$field_state."\n";
$body_message .= 'Postal: '.$field_postal."\n";
$body_message .= 'Country: '.$field_country."\n";
$body_message .= 'Profession: '.$field_profession."\n";
$body_message .= 'Industry: '.$field_industry."\n";
$body_message .= 'Job: '.$field_job."\n";
$body_message .= 'LinkedIn: '.$field_linkedin."\n";
$body_message .= 'Facebook: '.$field_facebook."\n";
$body_message .= 'Instagram: '.$field_instagram."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('CourtSide House. Thank you for your submission');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to membership@name.com');
        window.location = 'index.html';
    </script>
    <?php}?>

HTML

<pre><form method="post" action="contact.php">
                    <p>Contact<p>
                    <hr>
                        <div class="row half">
                            <div class="6u"><input type="text" class="text" placeholder="First Name*" name="first_name"></div>
                            <div class="6u"><input type="text" class="text" placeholder="Last Name*" name="last_name"></div>
                            <div class="12u"><input type="text" class="text" placeholder="Email*" name="cf_email"></div>
                            <div class="4u"><input type="text" class="text" placeholder="City*" name="cf_city"></div>
                            <div class="4u"><input type="text" class="text" placeholder="State/Province*" name="cf_state"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Postal Code*" name="cf_postal"></div>
                            <div class="12u"><input type="text" class="text" placeholder="Country*" name="cf_country"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Profession*" name="cf_profession"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Industry*" name="cf_industry"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Job Title*" name="cf_job"></div>
                        </div>
                        <p>Share Some Information About Yourself*<i><small class="small" id="small" style="font-size: 12px">(Please Place Link of Social Media Profile)</small></i></p>
                            <hr>
                        <div class="row half">


                            <div class="4u"><input type="text" class="text" placeholder="LinkedIn Profile*" name="cf_linkedin"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Facebook Profile*" name="cf_facebook"></div>
                            <div class="4u"><input type="text" class="text" placeholder="Instagram Profile*" name="cf_instagram"></div>
                            </div
                        ><div class="row half">
                            <div class="12u"><textarea name="message" placeholder="Message" ></textarea></div>
                        </div>
                        <div class="row">
                            <div class="12u">
                                <ul class="actions">
                                    <li><input type="submit" class="button" value="Submit"></li>

                                </ul>
                            </div>
                        </div>
                    </form></pre>

4 个答案:

答案 0 :(得分:0)

您需要附加到变量$ body_message,而不是像第二次调用时一样覆盖它。

$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";

应该是

$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n";

答案 1 :(得分:0)

可能您的电子邮件客户端容易混淆:

$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";

答案 2 :(得分:0)

1)您在变量赋值中缺少连接运算符:

$body_message = 'From: '.$field_name."\n";
$body_message = 'From: '.$field_last."\n";

应该是

$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n";
            ^^^^^
             HERE

2)您的消息字段名为message

$field_message = $_POST['cf_message'];

应该是

$field_message = $_POST['message'];

或更改

<textarea name="message" placeholder="Message" >

<textarea name="cf_message" placeholder="Message" >

答案 3 :(得分:0)

消息未显示,因为您在检索时使用了错误的密钥。

使用以下内容进行更新:

$field_message = $_POST['message']; // Textarea's name is message not cf_message

然后,由于您在$body_message分配$body_message后正在覆盖'From: '.$field_name."\n";,因此未显示名字。

更新如下:

$body_message = 'From: '.$field_name."\n";
$body_message .= 'From: '.$field_last."\n";  // Use .= not just =