我看到"喜欢" this demo blog中的功能,我试图创建一个类似的按钮。似乎一旦徘徊在这个心形按钮上,就会有一个"完成" class添加到包含它的div中,因此必须有一个JavaScript计数每个像。我想在每次访问时(而不是更多)按下鼠标点击它,并在重新加载页面时记住它(所以我想应该还有一个cookie?)。
答案 0 :(得分:0)
完成了ajax,它将数据存储到数据库而不刷新页面并同时更新值+1
答案 1 :(得分:0)
<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";
?>