Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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 如何创建一个Likes按钮来统计每个访问者的点击次数_Javascript_Jquery_Html_Css_Blogger - Fatal编程技术网

Javascript 如何创建一个Likes按钮来统计每个访问者的点击次数

Javascript 如何创建一个Likes按钮来统计每个访问者的点击次数,javascript,jquery,html,css,blogger,Javascript,Jquery,Html,Css,Blogger,我在中看到了likes函数,我正在尝试创建一个类似的按钮。看起来,一旦将鼠标悬停在这个心脏按钮上,就会有一个finish类添加到包含它的div中,因此必须有一个JavaScript来计算每个类的数量。我想让它存储每个访问者的喜好,而不是更多地点击鼠标,并在重新加载页面时记住它,所以我想也应该有一个cookie?这是用ajax完成的,它将数据存储到DB而不刷新页面,同时更新值+1index.html html文件 like_counter.php文件 您需要从JS向服务器发送一个请求,该服务器在数

我在中看到了likes函数,我正在尝试创建一个类似的按钮。看起来,一旦将鼠标悬停在这个心脏按钮上,就会有一个finish类添加到包含它的div中,因此必须有一个JavaScript来计算每个类的数量。我想让它存储每个访问者的喜好,而不是更多地点击鼠标,并在重新加载页面时记住它,所以我想也应该有一个cookie?

这是用ajax完成的,它将数据存储到DB而不刷新页面,同时更新值+1

index.html html文件 like_counter.php文件
您需要从JS向服务器发送一个请求,该服务器在数据存储中记录类似的内容。然后,您可以根据需要从数据存储中请求这些记录的数量。因此,您的问题太广泛。请向我们展示您的代码或创建一个提琴,我们可以复制您的问题并查看您的研究成果。除非你这么做,否则我们只能猜测。这不是一个一般性的讨论论坛。-请查看关于如何发布好问题的帮助!
<html>
<head>
 <script src="script.js"></script>
</head>
<body>
  <button id="btn">Like</button>
</body>
</html>
window.onload = function(){
        document.getElementById('btn').addEventListener("click",
                  function(){
                   sendLike();
                   document.getElementById("btn").disabled = true;  
                  });
}

function sendLike(){
  var xhr;
  if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
  else xhr = new ActiveXObject("Microsoft.XMLHTTP");
  xhr.open("GET","like_counter.php?like=1",true);
  if (xhr.readyState == 4 && xhr.status == 200) {
    //handle what you want to do after the operation is completed here
  }
  xhr.send();
}
<?php
 if(!(isset($_GET['like'])&&$_GET['like']==1)){
   die("Access denied!");
 }

 //This is just a demo. On a more practical situation,
 //you would want to store the likes in a database and verify the authenticity
 //of the request to prevent Cross-Site-Request-Forgery
 session_start();
 if(!isset$_SESSION['likes']) $_SESSION['likes'] = 0;
 $_SESSION['likes'] += 1;
 echo "done";
?>