点击html按钮发送电子邮件

时间:2016-02-11 05:00:46

标签: javascript php html email

我想在点击html按钮后立即发送电子邮件。为了完成这项任务,我已经编写了以下代码

HTML code:

<button onclick="sendEmail()">Send Email</button>
<p id="mailStatus"></p>

Java脚本代码:

function sendEmail()
{
     $.ajax({
           url: "mail.php",
           type: "POST",
           success: function(response) {
               if (!response) {
                    alert("Something went wrong. Please try again");
                    return;
               }

               var parsedJSON = eval('('+response+')');

               // If there's an error, display it.
               if(parsedJSON.Error) {
                  // Handle session timeout.
                  if (parsedJSON.Error == "Timeout") {
                       alert("Session timed out. Please login again.");
                       window.location.reload();
                   }
                }
               document.getElementById('mailStatus').innerHTML = "Email Sent successfully";  
            }
     });
}

问题是,当我点击发送电子邮件按钮时,我收到一条错误消息

"Uncaught ReferenceError: $ is not defined"

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

由于$.ajax是jQuery的功能,因此您需要在文件中添加jQuery。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

sendMail函数上方添加此行。