PHP表单正在发送空白电子邮件

时间:2015-02-15 02:30:30

标签: php jquery html ajax email

我看了其他的答案,我没有得到答案。我正在制作一个简单的HTML表单,其中包含一个发送电子邮件的PHP脚本。简单。除了它没有工作。这是HTML:

<form action="" method="post" id="contactform">
    <fieldset>
        <legend>Contact Form</legend>
        <p><label>First Name: </label><input type="text" name="first_name" class="text" required></p>
        <p><label>Last Name: </label><input type="text" name="last_name" class="text" required></p>
        <p><label>Email: </label><input type="text" name="email" class="text" required></p>
        <p><label>Message:</label><textarea rows="5" name="message" cols="30" class="message" required></textarea></p>
        <input type="submit" name="submit" value="Submit" class="submit">
    </fieldset>
</form>

这是AJAX:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $("#contactform").submit(function(){
      $.get("contactsend.php", function(data){
        alert(data);
      })
      return false;
    })
</script>

这是contactsend.php:

<?php 
$to = "shubhang.desai@gmail.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

$headers = "From: " . $from;
if (mail($to,$subject,$message,$headers)) {
    echo "Mail sent.";
} else {
    echo "An error occured. Try again later.";
}

&GT;

单击“提交”时,页面会显示“已发送邮件”,但电子邮件显示为空白。我不确定为什么数组_POST会为空。有人可以帮帮我吗?

提前致谢:)

4 个答案:

答案 0 :(得分:0)

尝试:

$date = date( 'r' );

$phpversion = phpversion();

$headers = "From: ".$from."\nDate: ".$date."\nX-Mailer: PHP v".$phpversion."\nMIME-Version: 1.0\nContent-Type: text/html; charset=iso-8859-1\nContent-Transfer-Encoding: 8bit";

答案 1 :(得分:0)

您的问题是您还没有通过ajax向服务器发送任何数据。

试试这个:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$("#contactform").submit(function(){
  jQuery.ajax({
        type: "POST",
        url: "contactsend.php",
        data: jQuery(this).serialize(),
        success: function(data){
            alert(data);
        }
    });
    return false;
})
</script>

腓:

<?php 
$to         = "shubhang.desai@gmail.com"; // this is your Email address
$from       = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name  = $_POST['last_name'];
$subject    = "Form submission";
$message    = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];

$headers    = 'MIME-Version: 1.0' . "\r\n";
$headers   .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers   .= 'From: ' .$from. "\r\n";

if (mail($to,$subject,$message,$headers)) {
    echo "Mail sent.";
} else {
    echo "An error occured. Try again later.";
}

答案 2 :(得分:0)

您错过了将表单中的值发送到您的服务器 您可以使用.serialize()获取值。它会将它们转换为具有正确编码值的字符串。

$("#contactform").submit(function () {
    $.get("contactsend.php", $(this).serialize(), function(data) {
        alert(data);
    });
    return false;
});

答案 3 :(得分:0)

您没有发送任何数据,请尝试此

<script      src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$("#contactform").submit(function(){
var details = $(this).serialize();
  $.get("contactsend.php",details, function(data){
    alert(data);
  })
  return false;
})
</script>

“serialize”函数自动为表单创建查询字符串,您也可以序列化各个输入字段。