【问题标题】:Send email on html button click [duplicate]在html按钮上发送电子邮件单击[重复]
【发布时间】:2016-05-21 17:51:24
【问题描述】:

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

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"

有人可以帮忙吗?

【问题讨论】:

  • 你添加了jquery吗?
  • no.. 因为我是新手,我应该在哪里添加 jquery???
  • var parsedJSON = eval('('+response+')'); 为什么要 eval?
  • @RaviSingh 您必须在脚本文件中添加 Jquery 库。以下是 Jquery 参考链接cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js
  • 感谢@Steve 和 Divyesh。 Vicky,我复制了 JS 代码,没有意识到 eval。我现在就修。。谢谢大家。

标签: javascript php html email


【解决方案1】:

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

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

sendMail函数上方添加这一行。

【讨论】:

  • 太棒了..谢谢队友..这解决了我的问题。
猜你喜欢
  • 2011-08-04
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
相关资源
最近更新 更多