关于javascript:使用mail()PHP脚本的jQuery AJAX表单发送电子邮件,但HTML表单的POST数据未定义

jQuery AJAX form using mail() PHP script sends email, but POST data from HTML form is undefined

感谢您抽出宝贵的时间来看看,伙计们。我正在使用jQuery创建一个非常基本的AJAX联系人表单。电子邮件发送了,但是打开电子邮件后没有POST数据,因此我只得到了在PHP脚本中定义的字符串。在我手机的电子邮件客户端上,电子邮件内容的字面意思是"未定义"。我尝试添加不同类型的标头数据无济于事,并且在PHP mail()函数上添加了许多变体。

我非常愿意为简单的AJAX表单采用更简单的解决方案,因此在此先感谢您提供任何新方法。

形式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   <section id="left">
      <label for="form_name">Name</label>
      <input name="form_name" id="form_name" type="text">

      <label for="form_email">Email</label>
      <input name="form_email" id="form_email" type="email">
   </section>

   <section id="right">
      <label for="form_msg">Message</label>
      <textarea name="form_msg" id="form_msg"></textarea>
      <input id="submit" class="button" name="submit" type="submit" value="Send">
   </section>

</form>

jQuery AJAX:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$(function() {
    $("#contact .button").click(function() {
        var name = $("#form_name").val();
        var email = $("#form_email").val();
        var text = $("#msg_text").val();
        var dataString = 'name='+ name + '&email=' + email + '&text=' + text;

        $.ajax({
            type:"POST",
            url:"email.php",
            data: dataString,
            success: function(){
            $('.success').fadeIn(1000);
            }
        });

        return false;
    });
});

PHP脚本(外部文件'email.php'):

1
2
3
4
5
6
7
8
9
10
<?php
if($_POST){
    $name = $_POST['form_name'];
    $email = $_POST['form_email'];
    $message = $_POST['form_msg'];

//send email
    mail("[email protected]","This is an email from:" .$email, $message);
}
?>

不需要输入查询字符串。只要将您的值放在一个对象中,jQuery就会为您处理其余的工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
var data = {
    name: $("#form_name").val(),
    email: $("#form_email").val(),
    message: $("#msg_text").val()
};
$.ajax({
    type:"POST",
    url:"email.php",
    data: data,
    success: function(){
        $('.success').fadeIn(1000);
    }
});


保留您的email.php代码不变,但替换以下JavaScript代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
 var name = $("#form_name").val();
        var email = $("#form_email").val();
        var text = $("#msg_text").val();
        var dataString = 'name='+ name + '&email=' + email + '&text=' + text;

        $.ajax({
            type:"POST",
            url:"email.php",
            data: dataString,
            success: function(){
            $('.success').fadeIn(1000);
            }
        });

与此:

1
2
3
4
5
6
7
8
    $.ajax({
        type:"POST",
        url:"email.php",
        data: $(form).serialize(),
        success: function(){
        $('.success').fadeIn(1000);
        }
    });

以便您的表单输入名称匹配。


您使用了错误的发布参数:

1
2
3
4
    var dataString = 'name='+ name + '&email=' + email + '&text=' + text;
                      ^^^^-$_POST['name']
                                       ^^^^--$_POST['name']
                                      etc....

javascript / html ID与实际的POST无关,尤其是当您构建自己的数据字符串并且不使用这些ID时。


您使用了错误的参数名称,请尝试:

1
2
3
4
5
6
7
8
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

//send email
    mail("[email protected]","51 Deep comment from" .$email, $message);
}


您的代码应为:

1
2
3
4
5
   <section id="right">
      <label for="form_msg">Message</label>
      <textarea name="form_msg" id="#msg_text"></textarea>
      <input id="submit" class="button" name="submit" type="submit" value="Send">
   </section>

Js

1
2
3
4
5
6
7
8
9
10
11
12
13
var data = {
    name: $("#form_name").val(),
    email: $("#form_email").val(),
    message: $("#msg_text").val()
};
$.ajax({
    type:"POST",
    url:"email.php",
    data: data,
    success: function(){
        $('.success').fadeIn(1000);
    }
});

PHP:

1
2
3
4
5
6
7
8
9
10
<?php
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

//send email
    mail("[email protected]","My Subject:",$email,$message);
}
?>

您的PHP脚本(外部文件\\'email.php \\')应如下所示:

1
2
3
4
5
6
7
8
9
10
<?php
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

//send email
    mail("[email protected]","51 Deep comment from" .$email, $message);
}
?>