How to create your OWN Like button? (Not facebook related)

前端 未结 3 1489
忘掉有多难
忘掉有多难 2021-01-06 05:35

I looked on SO, here, a few other places & ... cluelessness set\'s in.

What is the structure of the Facebook \" Like \" Button found all over websites ?

3条回答
  •  囚心锁ツ
    2021-01-06 06:09

    I've done this successfully. Here's how i did it:

    You have a table in a database called "likes" with fields username,postid (and date, id if you want too)

    Each post/blog/article should have its own id.

    When someone likes a post/blog/article, you take the id of the post/blog/article and the username who liked it and check the likes database to see if it already exists. If it already exists, you remove the like. If it does not exist you add a like.

    For example with AJAX, this is how i did it specifically:

    I have a blog post with id 6.

    Jonathan likes this post.

    These 2 variables are sent via a post form and wait for a response, likes logic checks database to see if this record already exists in likes table (username,postid) values ('Jonathan', 6) if the response is 1 (or true), then i update the div number for the likes button from whatever value was there originally and add 1. If the response is 0 (or false) then i take the original value in the likes counter and remove 1.

提交回复
热议问题