jQuery/PHP邮件发送的简便方法? [英] jQuery/PHP mail send the easy way?
本文介绍了jQuery/PHP邮件发送的简便方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,总而言之:
JavaScript:
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已发送".而且永远不会发送实际的邮件.
Works smoothly (shows every value in alert), but never shows div "sent". And never sends an actual mail.
sendmail.php在正确的位置,其代码是:
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();
?>
更新的jQuery代码也不起作用:
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;
});
推荐答案
您应该使用Ajax.它容易得多.这是一个有关使用PHP,Ajax和&发送邮件的简单教程. jQuery:
You should use Ajax. Its a lot easier. Here is a simple tutorial here on sending mail using PHP, Ajax, & jQuery:
它看起来类似于以下内容:
it would look something similar to this:
var data = "address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message;
//or however u want to format your data
$.ajax({
type: "POST",
url: "sendmail.php",
data: data,
success: function(phpReturnResult){
alert('Success: ' + phpReturnResult);
jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible. PHP Script's return result: " + phpReturnResult + "</p></div>");
},
error: function(errormessage) {
//you would not show the real error to the user - this is just to see if everything is working
alert('Sendmail failed possibly php script: ' + errormessage);
}
});
在您的PHP文件中,您似乎也两次使用了邮件功能.
Also in your PHP file, you seem to used the mail function twice.
这篇关于jQuery/PHP邮件发送的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文