通过单击html按钮发送电子邮件


Send email on html button click

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

网页代码:

<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"

有人可以帮忙吗?

因为$.ajax是jQuery的函数,所以你需要在你的文件中添加jQuery。

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

在函数上方添加此行sendMail