jQuery/PHP 邮件发送的简单方法?
好吧,长话短说:
JavaScript:
jQuery("submit").click(function() {
var address = jQuery("#mail").val();
var title = jQuery("#title").val();
var name = jQuery("#name").val();
var mail = jQuery("#email").val();
var message = jQuery("#msg").val();
alert(address);
alert(title);
alert(name);
alert(mail);
alert(message);
jQuery.post("sendmail.php",
{address: address, title: title, name: name, mail: mail , message: message},
function(data){
jQuery("#email").html("<div id='sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>");
alert('sent');
});
return false;
});
工作顺利(显示警报中的每个值),但从不显示 div“已发送”。并且从不发送实际的邮件。
sendmail.php 位于正确的位置,它的代码是:
<?php
// getting variables from form
$emailTo = trim($_REQUEST['address']);
$subject = trim($_REQUEST['title']);;
$name = trim($_REQUEST['name']);
$emailFrom = trim($_REQUEST['mail']);
$message = $_REQUEST['message'];
// prepare email body text
$Body = "You have a message from: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= $message;
// send prepared message
$sent = mail($emailTo, $subject, $Body);
//callback for jQuery AJAX
if ($sent){
echo 'sent';
}
else{}
print_r($_REQUEST); die();
?>
更新了 jQuery 代码,也不起作用:
jQuery("#contact-form-send").click(function() {
var address = jQuery("#contact-form-mail-address").val();
var title = jQuery("#contact-form-mail-title").val();
var name = jQuery("#contact-form-name").val();
var mail = jQuery("#contact-form-email").val();
var message = jQuery("#contact-form-message").val();
var data = "&address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message;
jQuery.ajax({
type: "POST",
url: "sendmail.php",
data: data,
success: function(){
jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>");
}
});
return false;
});
Ok, to make long story short:
JavaScript:
jQuery("submit").click(function() {
var address = jQuery("#mail").val();
var title = jQuery("#title").val();
var name = jQuery("#name").val();
var mail = jQuery("#email").val();
var message = jQuery("#msg").val();
alert(address);
alert(title);
alert(name);
alert(mail);
alert(message);
jQuery.post("sendmail.php",
{address: address, title: title, name: name, mail: mail , message: message},
function(data){
jQuery("#email").html("<div id='sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>");
alert('sent');
});
return false;
});
Works smoothly (shows every value in alert), but never shows div "sent". And never sends an actual mail.
sendmail.php is in the right place, and it's code is:
<?php
// getting variables from form
$emailTo = trim($_REQUEST['address']);
$subject = trim($_REQUEST['title']);;
$name = trim($_REQUEST['name']);
$emailFrom = trim($_REQUEST['mail']);
$message = $_REQUEST['message'];
// prepare email body text
$Body = "You have a message from: ";
$Body .= $name;
$Body .= "\n";
$Body .= "\n";
$Body .= $message;
// send prepared message
$sent = mail($emailTo, $subject, $Body);
//callback for jQuery AJAX
if ($sent){
echo 'sent';
}
else{}
print_r($_REQUEST); die();
?>
Updated jQuery code, also doesn't work:
jQuery("#contact-form-send").click(function() {
var address = jQuery("#contact-form-mail-address").val();
var title = jQuery("#contact-form-mail-title").val();
var name = jQuery("#contact-form-name").val();
var mail = jQuery("#contact-form-email").val();
var message = jQuery("#contact-form-message").val();
var data = "&address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message;
jQuery.ajax({
type: "POST",
url: "sendmail.php",
data: data,
success: function(){
jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>");
}
});
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该使用 Ajax。它容易得多。 ~~这里有一个关于使用PHP、Ajax、&发送邮件的简单教程。 jQuery:~~
[使用 PHP、Ajax 和 jQuery 发送邮件]
编辑:链接不再可用
它看起来与此类似:
也在您的 PHP 文件中,您似乎使用了两次邮件功能。
You should use Ajax. Its a lot easier. ~~Here is a simple tutorial here on sending mail using PHP, Ajax, & jQuery:~~
[Send mail using PHP, Ajax and jQuery]
Edit: the link is no more available
it would look something similar to this:
Also in your PHP file, you seem to used the mail function twice.