我有一个表格,上面有姓名和邮件的输入。守则如下:
<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;表格提交?对于一些帮助和见解,我将不胜感激。
答案 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');
}
});
});
});