Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在html上发送电子邮件按钮单击_Javascript_Php_Html_Email - Fatal编程技术网

Javascript 在html上发送电子邮件按钮单击

Javascript 在html上发送电子邮件按钮单击,javascript,php,html,email,Javascript,Php,Html,Email,我想在单击html按钮后立即发送电子邮件。为了完成这项任务,我编写了以下代码 HTML代码: <button onclick="sendEmail()">Send Email</button> <p id="mailStatus"></p> function sendEmail() { $.ajax({ url: "mail.php", type: "POST", suc

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

HTML代码:

<button onclick="sendEmail()">Send Email</button>
<p id="mailStatus"></p>
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";  
            }
     });
}
问题是,当我点击发送电子邮件按钮时,我收到一条错误消息

“未捕获引用错误:$未定义”


有人能帮忙吗???

就像
$。ajax
是jQuery的函数,所以您需要在文件中添加jQuery

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


将这一行添加到sendMail函数上方。

是否添加了jquery?否。。由于我对这件事还不熟悉,我应该在哪里添加jquery???var parsedJSON=eval(“(“+response+”)”);为什么要eval?@RaviSingh您必须在脚本文件中添加Jquery库。下面是Jquery参考链接,谢谢@Steve和Divyesh。Vicky,我复制了JS代码,没有意识到eval。我现在就把它修好。。谢谢大家,太棒了。。谢谢你,伙计。。这解决了我的问题。