JavaScript代码不会使用ajax发送电子邮件

时间:2014-02-07 16:45:53

标签: javascript php jquery ajax

我有一个JS代码如下:

function validateForm(){


 // some code 
 $.get("../sendMail.php");

 alert('Reached here ? then the mail was sent successfully');
 // more stuff

}

sendMail.php代码:

<?php
$to = "someone@gmail.com";
$subject = "MY PHP MESSAGE";

$name = $_REQUEST['myName"'];
$phone = $_REQUEST['myPhone'];
$email = $_REQUEST['myEmail'];

$message .= "<br>"."<br>";

$message .= "<strong><font color='red'>Information Below.</font></strong>"."<br>"."<br>";

$message .= "<strong>Name:</strong> ".$name ."<br/>";
$message .="<strong>Phone:</strong> ".$phone."<br/>";
$message .="<strong>Email:</strong> ".$email."<br/>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: michaeljackson@gmail.com' . "\r\n";

mail($to,$subject,$message,$headers);   
?>

即使JS中的警报工作得很好,也不会发送邮件。

知道代码有什么问题吗?

非常感谢

(仅供参考,我在本地主机上运行,​​如果它有任何区别的话)

编辑:

$.ajax({
    url: '../sendMail.php',
    success: function(data, textStatus, jqXHR){
        alert(console.log('data: %O', data));
        alert(console.log('textStatus: %s', textStatus));
        alert(console.log('jqXHR: %O', jqXHR));
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert(console.log('jqXHR: %O', jqXHR));
        alert(console.log('textStatus: %s', textStatus));
        alert(console.log('errorThrown: %s', errorThrown));
    }});

但没有发送任何内容,屏幕上也没有打印任何内容。没有警报,没有。

3 个答案:

答案 0 :(得分:3)

jQuery的.get方法是异步。这意味着无论AJAX调用是否成功,都会出现alert。你需要的是:

$.get('../sendMail.php', function(data){
    console.log('Reached here ? then the mail was sent successfully');
}):

顺便说一下,.get.ajax的便利别名。如果您遇到问题,则应使用.ajax,这为您提供了更多调试选项:

$.ajax({
    url: '../sendMail.php',
    success: function(data, textStatus, jqXHR){
        console.log('data: %O', data);
        console.log('textStatus: %s', textStatus);
        console.log('jqXHR: %O', jqXHR);
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log('jqXHR: %O', jqXHR);
        console.log('textStatus: %s', textStatus);
        console.log('errorThrown: %s', errorThrown);
    }
});

有关您可以使用的所有选项,请参阅documentation for .ajax

答案 1 :(得分:0)

您还缺少要发送到PHP文件的参数,它正在等待接收:

$name = $_REQUEST['myName"'];
$phone = $_REQUEST['myPhone'];
$email = $_REQUEST['myEmail'];

所以你也应该根据请求发送它们:

$.ajax({
  type: "POST",
  url: "../sendMail.php",
  data: { myName: "John Doe", myPhone: "123456789", myEmail: "loller@dude.com" }
}).done(function( msg ) {
   alert( "Mail sent: " + msg );
});

我建议您了解AJAX请求以及jQuery如何做到这一点:

另请参阅PHP如何处理$ _REQUEST var:

电贺!

答案 2 :(得分:-1)

使用这个,

var myName = $('#name').val(); //get from input fields
var myEmail = $('#email').val();
var myPhone = $('#phone').val();
$.ajax({
  url: "../sendMail.php", //url to send
  type:"get", //get or post
  data: "myName="+myName+"&myEmail="+myEmail+"&myPhone="+myPhone, //data to be send
  success:function(response){ //on success
    alert('Reached here ? then the mail was sent successfully');
  },
  error: function(response){ //on error
    alert(response);
  }
});