jQuery的AJAX的使用形式的邮件()的PHP脚本发送电子邮件,但是从HTML表单POST数据不确定 [英] jQuery AJAX form using mail() PHP script sends email, but POST data from HTML form is undefined

查看:227
本文介绍了jQuery的AJAX的使用形式的邮件()的PHP脚本发送电子邮件,但是从HTML表单POST数据不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出时间来看看,伙计们。我创建使用jQuery一个pretty的基本AJAX的联系表格。电子邮件发送,但在打开电子邮件没有POST数据,所以我只是让我在PHP脚本中定义的字符串。在我的手机上的电子邮件客户端,邮件的内容,字面意思是不确定。我试着将不同类型的报头数据都无济于事,和若干变PHP的mail()函数。

我更愿意采用一个简单的AJAX形式的简单的解决方案,所以在此先感谢任何新的方法。

下面的表格:     

 <节ID =左>
      <标签=FORM_NAME>名称< /标签>
      <输入名称=FORM_NAMEID =FORM_NAME类型=文本>

      <标签=form_email>电子邮件< /标签>
      <输入名称=form_emailID =form_emailTYPE =电子邮件>
   < /节>

   <节ID =权与GT;
      <标签=form_msg>消息< /标签>
      < textarea的名字=form_msgID =form_msg>< / textarea的>
      <输入ID =提交级=按钮NAME =提交类型=提交值=发送>
   < /节>

< /形式GT;
 

jQuery的AJAX:

  $(函数(){
    $(#联系.button)。点击(函数(){
        变种名称= $(#FORM_NAME)VAL()。
        变种的电子邮件= $(#form_email)VAL()。
        VAR文字= $(#msg_text)VAL()。
        VAR dataString =NAME ='+名字+'和;电子邮件='+电子邮件+'和;文本='+文字;

        $阿贾克斯({
            键入:POST,
            网址:email.php
            数据:dataString,
            成功:函数(){
            $('成功')淡入(1000)。
            }
        });

        返回false;
    });
});
 

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

 < PHP
如果($ _ POST){
    $ NAME = $ _ POST ['FORM_NAME'];
    $电子邮件= $ _ POST ['form_email'];
    $消息= $ _ POST ['form_msg'];

//发电子邮件
    邮件(j.andrew.sears@gmail.com,$电子邮件,$消息,从51深度评论);
}
?>
 

解决方案

有没有必要做一个查询字符串。只要把你的价值观中的对象和jQuery会照顾其余的为您服务。

  VAR数据= {
    名称:$(#FORM_NAME)VAL()。
    电子邮件:$(#form_email)VAL()。
    消息:$(#msg_text)VAL()。
};
$阿贾克斯({
    键入:POST,
    网址:email.php
    数据:数据,
    成功:函数(){
        $('成功')淡入(1000)。
    }
});
 

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.

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

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:

$(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
if($_POST){
    $name = $_POST['form_name'];
    $email = $_POST['form_email'];
    $message = $_POST['form_msg'];

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

解决方案

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.

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

这篇关于jQuery的AJAX的使用形式的邮件()的PHP脚本发送电子邮件,但是从HTML表单POST数据不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆