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

时间:2022-10-08 21:44:37

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@gmail.com", "This is an email from:" .$email, $message);
}
?>

6 个解决方案

#1


18  

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

#2


6  

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.

这样您的表单输入名称就匹配了。

#3


4  

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

#4


4  

You are using the wrong parameters name, try:

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

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

//send email
    mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}

#5


2  

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@gmail.com","My Subject:",$email,$message);
}
?>

#6


0  

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("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}
?>

#1


18  

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

#2


6  

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.

这样您的表单输入名称就匹配了。

#3


4  

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

#4


4  

You are using the wrong parameters name, try:

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

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

//send email
    mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}

#5


2  

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@gmail.com","My Subject:",$email,$message);
}
?>

#6


0  

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("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
}
?>