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

前端 未结 3 1490
忘掉有多难
忘掉有多难 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:01

    You must first have a database where you can store various values. Now, Whenever the user clicks the button, the value of the button stored in the database must be incremented. For this, you will need a backend language which connects you to SQL database. So whenever the button has clicked the value of the likes in the database changes.

    0 讨论(0)
  • 2021-01-06 06:07

    To answer your question consider what happens with buttons and then go into like buttons.

    1. Pressing a button triggers an event on client which may or may not update a server somewhere to notify that a button has been pressed for such and such intention. You can send a lot of extra info with this event like when and where who and why etc
    2. Like buttons usually have extra info on who liked it and what they like. In order to get that you might ask people to sign in or provide some kind of input to identify them.

    Take a real world example of a like button you can implement in say javascript using any server side technology

    1. Whoever install your script will be able to see the button. You may form it with any css or your javascript can simply load an iFrame from your server or append elements to DOM to show this button
    2. When clicked it calls your server with person's info or at least the page url where it was called. For example google analytics uses a unique ID associated with domain url to track visitors.
    3. when you recieve this call you can update your database/storage or anything with the tick mark that button on abc site has been pressed so lets update their likes or dislikes.
    4. If you want your javascript can also increment the number on the same page either before or after updating your server.
    5. When someone else visit that site the script again loads and send a request to your server so you can update the count on page but this time user does not click on like/dislike button so you dont update the record.
    6. You may then show it as a pie chart to user on total visits to their site or page with division in people who liked it and people who did not report back (did not press the button)

    If you are still wondering how you can create a button . Use CSS button generator to get one

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题