favorite button for article

1k views Asked by At

I want to create for my blog an articles fav button. First I use :

<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>

Where p is post ID and u is the user who fav'd the article. In the loop for the articles I add an image with:

onclick="AddPost(<php echo of the post id>, <php echo of the current user id>)"

And that was stupid because the function works for all of them, not for just one. In addfav.php I just get the p and u parameters and then INSERT into the database. I'm new to Ajax and I dont know how to make it different for the articles.

1

There are 1 answers

0
webbiedave On

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.