weixin_33705053 2016-02-02 08:53 采纳率: 0%
浏览 12

通过邮件发送div内容

i have a form with inputs for name and mail. The Code looks like this:

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

Additionally i need to pass some div content (.inner) to the .php mailer. Code looks like this:

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

The .php mailer:

$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' . "
";
$headers .= 'Content-type: text/html; charset=utf-8' . "
";
$headers .= "From: $email
";

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

After submitting i'll get an email with only the ajax script submitted $_POST['message']; .. $email and $name wont show, not in the $test area nor will the email be displayed in the mail header. My guess is that the ajax script overrides the "normal" form submit? For some help and insights i would be grateful.

  • 写回答

1条回答 默认 最新

  • weixin_33714884 2016-02-02 08:58
    关注

    In your data you are sending only message:

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

    You can check what all has been sent by putting a var_dump():

    var_dump($_POST);
    

    You need to add the others too to get there. I believe you should have got the error saying, undefined index. So change it to:

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

    Finally, your full code should be like:

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作