Click counting, limiting, and disabling
Is it possible to use JQuery & PHP to create a "like" button that a user could click and it would add +1 to a "number of likes" database (or even text file) and disable the "like" button for that user so that the user could only click it once? I was browsing around and found some information about writing cookies with JQuery:
http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html
Perhaps, when a like button is clicked, it could write a cookie to the user's computer that would prevent them from future clicks? It just basically needs to be that the user could click the like button, it adds a count to some type of database, and it disables the button for the user. Pretty simple I wou开发者_StackOverflowld imagine - there may already be some type of plugin for this, but I haven't found any. Any ideas?
Thanks!
jquery:
$("button").click(function(){
$(this).remove();
$.post('count.php');
});
though the user can just reload the page, so any real validation needs to happen on the php side.
You may want to look at jQuery's one() function. It allows you to bind an event for only one invocation. Here's an example I'd run on page load.
if (likedBefore) {
$("button").addClass("liked");
}
else {
$("button").one("click", function() {
$(this).addClass("liked");
$.post("count.php");
});
}
Validating server side is a bit more difficult. It really depends on how secure you need this to be.
精彩评论