【问题标题】:jQuery/PHP mail send the easy way?jQuery/PHP 邮件发送简单的方法?
【发布时间】:2011-02-19 15:55:16
【问题描述】:

好吧,长话短说:

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

【问题讨论】:

  • @kjy112,当然,我正在努力解决这个问题 :) 我已经完全更新了第一篇文章,也许你能找到问题所在?但首先我将使用 .ajax 检查您的方法。
  • 你已经关闭了你的 else 语句,没有在里面放任何东西......也许是 print_r($_REQUEST);
  • @kjy112,没有任何改变,顺便说一句,我无法访问 sendmail.php 输出,我只是单击“提交”,然后出现了 div。而已。没有重定向到 sendmail.php。
  • 听起来很有效。它在做 ajax,它不会重定向而是根据请求提交。因此,如果您的 sendmail.php 正确,就好像您从不离开页面但仍发送邮件。基本上你想要做的是将回调信息附加到jQuery(".email-us".html(); 我将再次更新我的答案以显示你
  • @kjy112 它可能无法正常工作,因为我没有收到任何邮件... ;/ 但我是在本地主机上做的,我会尝试在其他地方检查服务器。无论如何,我喜欢将回调信息附加到正确的行,但我不确定如何。

标签: php javascript jquery post sendmail


【解决方案1】:

您应该使用 Ajax。它容易得多。 ~~这里有一个使用 PHP、Ajax 和 jQuery 发送邮件的简单教程:~~

[使用 PHP、Ajax 和 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 文件中,您似乎使用了两次邮件功能。

【讨论】:

  • Af!问题是“成功”永远不会发生!我从来没有见过一个名为“email-sent”的 div 和“Thank you for the ...”文本 :( 这就是重点。一切正常,所有数据都被抓取,链接被创建,sendmail.php 文件是在正确的地方(老实说,我什至几乎在所有地方都复制了它)并且什么都没有!
  • @anonymous 附上这个错误函数,看看它是否显示。如果它显示它可能是你的 php 脚本有问题。但是,如果确实如此,那么这意味着 ajax 是成功的
  • @anonymous 为成功或失败添加了警报,因此您将知道您的 ajax 是否失败或您的 php 脚本是否失败。祝你好运
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2015-06-24
相关资源
最近更新 更多