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;       
});  

解决方法:

你应该使用Ajax.它更容易.这里有一个关于使用PHP,Ajax和&amp ;;发送邮件的简单教程. jQuery的:

Send mail using PHP, Ajax and jQuery

它会看起来像这样:

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文件中,您似乎使用了两次邮件功能.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...