簡體   English   中英

JavaScript代碼不會使用ajax發送電子郵件

[英]JavaScript code won't send email using 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中的警報工作得很好,也不會發送郵件。

知道代碼有什么問題嗎?

非常感激

(僅供參考,我在localhost上運行,如果它有任何區別)

編輯:

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

但沒有發送任何內容,屏幕上也沒有任何內容。 沒有警報,沒有。

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

有關您可以使用的所有選項,請參閱.ajax文檔

您還缺少要發送到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:

映入眼簾!

使用這一個,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM