每封邮件发送div内容

时间:2016-02-02 08:53:16

标签: jquery html ajax forms

我有一个表格,上面有姓名和邮件的输入。守则如下:

<form role="form" method="post" action="email.php">
   <input type="text" class="sendmail" name="name" placeholder="Your name" />
   <input type="text" class="sendmail" name="email" placeholder="Your email" />
</form>

此外,我需要将一些div内容(.inner)传递给.php邮件程序。代码如下所示:

$(function() {
  $('.send').click(function(e) {
        $.ajax({
              url: 'email.php',
              type: 'POST',
              data: {'message': $('.inner').html()},
              success: function (data) {
                    alert('You data has been successfully e-mailed');
              }
        });
    });
});

.php邮件:     

$email = $_POST['email'];
$subject = "Subject";
$name = $_POST['name'];
$content = $_POST['message'];

$to = 'my@email.com';

$test = '
        <html>
        <head>
          <title>Email: '. $email .'</title>
        </head>
        <body>
            <p>Von: '. $name .'</p>
          <div>
            '. $content .'
          </div>
        </body>
        </html>
        ';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "From: $email\r\n";

mail($to, $subject, $test, $headers); 

提交后,我会收到一封电子邮件,其中只有提交的{j}脚本$_POST['message']; .. $email$name不会显示,不会显示在$test区域内电子邮件是否会显示在邮件标题中。我的猜测是ajax脚本会覆盖&#34; normal&#34;表格提交?对于一些帮助和见解,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

data仅发送message

data: {
  'message': $('.inner').html()
},

您可以通过var_dump()

来查看所有内容的发送方式
var_dump($_POST);

你也需要添加其他人才能到达那里。我相信你应该得到错误说,未定义的索引。所以改成它:

data: {
  'message': $('.inner').html(),
  'email':   $('[name="email"]').val(),
  'name':    $('[name="name"]').val()
},

最后,您的完整代码应该是:

$(function() {
  $('.send').click(function(e) {
    $.ajax({
      url: 'email.php',
      type: 'POST',
      data: {
        'message': $('.inner').html(),
        'email':   $('[name="email"]').val(),
        'name':    $('[name="name"]').val()
      },
      success: function(data) {
        alert('You data has been successfully e-mailed');
      }
    });
  });
});