1

我想为我的博客创建一个文章收藏按钮。首先我使用:

<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我只是获取pu参数然后INSERT进入数据库。我是 Ajax 的新手,我不知道如何让文章与众不同。

4

1 回答 1

1

Your PHP code needs to not allow any more favorites to be added (I cannot comment further on that because you did not include the PHP/SQL code). Also, in your javascript code, once AJAX has returned successful, disable the other Fav buttons.

By the way, using a well-tested library like jQuery (especially for AJAX) will greatly speed development.

于 2011-10-07T21:46:16.280 回答