除了ajax GET函数之外,我是否需要在javascript中包含任何代码,以便将变量传递给php脚本并运行它


Do I need to include any code in my javascript other than the ajax GET function in order to pass my variables to my php script and then run it?

这段代码似乎与我在各种论坛上看到的内容一致。据我所知,我没有任何语法错误,我的javascript似乎工作正常,但我没有收到任何电子邮件。

$("#Contact").click(function(){
    if (clicked === 1 && armed === 1){
        $.ajax({
            type: GET,
            url: Scripts/contact.php,
            data: {
                Email: EmailAddress,
                Subject: EmailSubject,
                Message: EmailMessage,
            }
        });
    }
});    
<?php
$email = $_GET['Email'];
$subject = $_GET['Subject'];
$message = $_GET['Message'];
$message = wordwrap($message, 70);
$headers .= 'From:' . $email . "'r'n";
mail("me@example.com", $subject, $message, $headers);
?>

我想我的jquery脚本中需要一行代码来调用我的php函数,但我没有这样做的经验,而且我在网上看到的一些事情让我的代码看起来可能会按原样工作。

很难说是什么原因导致了这里的问题,因为我没有看到您的整个脚本。但我建议您使用jQuery库$.get()中的简写函数,因为如果您只是在学习,它会更简单一些。

所以,你可以试试:

$("#Contact").click(function(){
  if (clicked === 1 && armed === 1){
    $.get('Scripts/contact.php?Email='+EmailAddress+'&Subject='+EmailSubject+'&Message='+EmailMessage, function(response) {
      alert(response);
    }
  }
});  

<?php
$email = $_GET['Email'];
$subject = $_GET['Subject'];
$message = $_GET['Message'];
$message = wordwrap($message, 70);
$headers .= 'From:' . $email . "'r'n";
// echo 'sent' if successfully sent, or 'not sent' otherwise
echo mail("me@example.com", $subject, $message, $headers) ? 'sent' : 'not sent';
?>

此外,请确保ajax语句中的路径是正确的,并且在引用变量名之前已经定义了所有变量名,并且没有拼写错误、大写错误等。如果ajax语句有效,并且与PHP脚本通信,并且它发出"未发送"警报,那么您就知道错误来自邮件脚本中的某个地方。

您需要将PHP脚本的URL用引号括起来:

 url: 'Scripts/contact.php',