我想为我的博客创建一个文章收藏按钮。首先我使用:
<script type="text/javascript">
function AddPost(str,user)
{
if(str == "")
{
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "addfav.php?p=" + str + "&u=" + user, true);
xmlhttp.send();
}
</script>
p
帖子 ID在哪里,并且u
是喜欢该文章的用户。在文章的循环中,我添加了一张图片:
onclick="AddPost(<php echo of the post id>, <php echo of the current user id>)"
这很愚蠢,因为该功能适用于所有人,而不仅仅是一个。在addfav.php
我只是获取p
和u
参数然后INSERT
进入数据库。我是 Ajax 的新手,我不知道如何让文章与众不同。