【问题标题】:PHP form is sending blank emailPHP表单正在发送空白电子邮件
【发布时间】:2015-02-15 02:30:29
【问题描述】:

我查看了其他 SO 答案,但没有得到答案。我正在使用发送电子邮件的 PHP 脚本制作一个简单的 HTML 表单。简单的。除了它不工作。这是 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.";
}

?>

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

提前致谢:)

【问题讨论】:

标签: php jquery html ajax email


【解决方案1】:

您的问题是您尚未通过 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:

<?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】:

    试试:

    $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";
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        你没有发送任何数据,试试这个

        <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>
        

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

        【讨论】: