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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15300470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:00:17  来源:igfitidea点击:

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

phpjavascriptjqueryajaxhtml

提问by Jason Sears

Thanks for taking the time to look, guys. I'm creating a pretty basic AJAX contact form using jQuery. The email sends, but upon opening the email there is no POST data, so I just get the strings I defined in the PHP script. On my phone's email client, the content of the email literally says 'undefined'. I've tried adding different types of header data to no avail, and a number of variations on the PHP mail() function.

谢谢你花时间看,伙计们。我正在使用 jQuery 创建一个非常基本的 AJAX 联系表单。电子邮件发送,但在打开电子邮件时没有 POST 数据,所以我只得到我在 PHP 脚本中定义的字符串。在我手机的电子邮件客户端上,电子邮件的内容字面意思是“未定义”。我尝试添加不同类型的标题数据但无济于事,并且对 PHP mail() 函数进行了许多变体。

I am more than willing to adopt an easier solution for a simple AJAX form, so thanks in advance for any new approaches.

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

Here is the form:

这是表格:

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

The jQuery AJAX:

jQuery AJAX:

$(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;
    });
});

The PHP script (external file 'email.php'):

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

<?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);
}
?>

回答by Seain Malkin

There is no need to make a query string. Just put your values in an object and jQuery will take care of the rest for you.

无需创建查询字符串。只需将您的值放在一个对象中,jQuery 将为您处理其余的事情。

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);
    }
});

回答by Jannie Theunissen

Leave your email.php code the same, but replace this JavaScript code:

保持您的 email.php 代码不变,但替换此 JavaScript 代码:

 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);
            }
        });

with this:

有了这个:

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

So that your form input names match up.

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

回答by Marc B

You're using the wrong post parameters:

您使用了错误的帖子参数:

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

The javascript/html IDs are irrelevant to the actual POST, especially when you're building your own data string and don't use those same IDs.

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

回答by Garis M Suero

You are using the wrong parameters name, try:

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

if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

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

回答by Hil

You code should be:

你的代码应该是:

   <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

JS

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);
    }
});

The PHP:

PHP:

<?php
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

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

回答by EmCo

Your PHP script (external file 'email.php') should look like this:

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

<?php
if($_POST){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['text'];

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