内容表单发送空白电子邮件

时间:2014-12-28 04:18:42

标签: php

我创建了一个联系表单。当我填写所有内容并单击提交,然后在gmail上查看我的电子邮件。所有部分都是空白的。 我收到的例子:

Name:
Email:
Subject:
Message:

这是我的HTML代码:

        <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
            <div class="row">
                <div class="col-sm-5">
                    <div class="form-group">
                        <input type="text" name="FirstName" id="FirstName" class="form-control" required="required" placeholder="First Name">
                    </div>
                    <div class="form-group">
                        <input type="text" name="LastName" id="LastName" class="form-control" required="required" placeholder="Last Name">
                    </div>
                    <div class="form-group">
                        <input type="text" name="Emailaddress" id="Emailaddress" class="form-control" required="required" placeholder="Email address">
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
                    </div>
                </div>
                <div class="col-sm-7">
                    <textarea name="Message" id="Message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
                </div>
            </div>
        </form>

这是我的php代码:

header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email sent!'
);

$FirstName = trim(stripslashes($_POST['FirstName']));
echo "Found Firstname: " . $FirstName;
$LastName = trim(stripslashes($_POST['LastName'])); 
echo "Found LastName: " . $LastName;
$Emailaddress = trim(stripslashes($_POST['Emailaddress'])); 
echo "Found Emailaddress: " . $Emailaddress;
$subject = 'This is the subject';
echo "Found subject: " . $subject;  
$Message = trim(stripslashes($_POST['Message']));
echo "Message: " . $Message;    

$email_from = $Emailaddress;
$email_to = 'karolburatynski@gmail.com';

$body = 'Name: ' . $FirstName . "\n\n" . 'Email: ' . $Emailaddress . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $Message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;

3 个答案:

答案 0 :(得分:2)

查找处理联系表单的javascript并替换此行:

$.post($(this).attr('action'), function(data) {

这个:

$.post($(this).attr('action'),$(this).serialize(), function(data) {

答案 1 :(得分:0)

你编码有问题这里是解决方案:

<?php
//print_r($_POST); die; 
//uncomment it if you would liketo do debug

    $FirstName = trim(stripslashes($_POST['FirstName']));
    $LastName = trim(stripslashes($_POST['LastName'])); 
    $Emailaddress = trim(stripslashes($_POST['Emailaddress'])); 
    $subject = 'This is the subject';
    $Message = trim(stripslashes($_POST['Message']));


    $email_from = $Emailaddress;
    $email_to = 'karolburatynski@gmail.com';

    $body = 'Name: ' . $FirstName . "\n\n" . 'Email: ' . $Emailaddress . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $Message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
  ob_end_flush();

   header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Email sent!'
    );
    echo json_encode($status);
    die;

你不应该以header开头('Content-type:application / json');.在这种情况下,整个页面被视为json。所以它不起作用。

答案 2 :(得分:0)

查找处理联系表单的javascript并替换此行:

    <?php require 'database.php';

    if (isset($_POST['wijzigen'])) {
     $id = $_POST['id'];
     $naam = $_POST['naam'];
     $datum = $_POST['datum'];
     $serienummer = $_POST['serienummer'];
     $simnummer = $_POST['simnummer'];
     $telnummer = $_POST['telnummer'];
     $pukcode = $_POST['pukcode'];
     $notities = $_POST['notities'];
     $sql = "UPDATE apparaten SET naam='$naam', datum='$datum', serienummer='$serienummer', simnummer='$simnummer', telnummer='$telnummer', pukcode='$pukcode', notities='$notities' WHERE id = $id";

     if ($db->query($sql) === TRUE) {
       ?>
       <script type="text/javascript">
        var timer = 0.1; //seconds
        website = "wijzig.php"
        function delayer() {
         window.location = website;
        }
         setTimeout('delayer()', 1000 * timer);
        </script>
         <?php
        } else {
         echo "Error: " . $sql . "<br>" . $db->error;
       }
     }
    ?>

对于这个:

$.post($(this).attr('action'), function(data) {

“更改此行然后邮件将工作