Problem with setting cookies to prevent multiple likes

by 2 replies
2
I have a website with movie quotes. The visitor can like a movie quote which adds 1 to it's ranking in a database. That code is working. I'm trying to write code that prevents a user from liking a quote multiple times but that code isn't working. You can see the site at this link Here is what I have so far. Can anyone tell me what I'm doing wrong?


Code:
function setLike(id) {
    document.cookie=id+'=y';
}

function hasLiked(id) {
    var cookies=document.cookie.split(';');
    for (var i=0;i<cookies.length;i++) {
        var cookie=cookies[i].split('=');
        if (cookie[0]==id) return true;
    }
    return false;
}

var button=document.getElementByClassName('like');

button.onclick=function SetLikeCookie() {
    var id=this.getAttribute('id');
    if (!hasLiked(id)) {
        alert('Your like has been recorded');
        setLike(id);
    } else {
        alert('You cant like a quote twice');
    }
}

My html is:


Code:
<div><p>"Frankly, my dear, I don't give a damn."</p>
 <p>Rhett Butler | Clark Gable | Added 9/28/2015</p>
 </div>
 <div class="movie-like-image">
 <img onclick="increment(this); SetLikeCookie;" id="1" class="like" src="../images/like.jpg">
 </div>
#programming #cookies #likes #multiple #prevent #problem #setting
  • I'm really needing help with this. It's the final thing I need to do before I start adding new pages to my site. I've been working on this for many hours but I'm new to javascript so I know I'm doing things wrong. Basically I've already got another javascript that calls a php file and that already uses the id of the image. I know I can't have 2 ids and I think I've discovered that a class will not work so I tried using a data attribute. Here is my latest attempt and it's still not working.

    Javascript:

    Code:
     function setLike(articleID) {
    				document.cookie=articleID+'=y';
    				}
    
    				function hasLiked(articleID) {
    					var cookies=document.cookie.split(';');
    					for (var i=0;i<cookies.length;i++) {
    						var cookie=cookies[i].split('=');
    						if (cookie[0]==articleID) return true;
    					}
    					return false;
    				}
    				
    				var buttonatt=document.getElementById('1');
    				var button = buttonatt.getAttribute("articleID");
    				
    				button.onclick=function() {
    					var articleID=this.getAttribute('articleID');
    					if (!hasLiked(articleID)) {
    						//register the like in your system
    						//...
    						//
    						setLike(articleID);
    					} else {
    						alert('You cant like or dislike an article twice');
    					}
    				}
    html:

    Code:
    <img onclick="increment(this)" id="1" articleID="1" src="../images/like.jpg">
    • [1] reply
    • I had a look at your page. You have javascript errors in your code. I could see this using the console tab in google chrome. Other browsers also have similar consoles and developer tools. You need to debug the page and resolve these errors first.

Next Topics on Trending Feed

  • 2

    I have a website with movie quotes. The visitor can like a movie quote which adds 1 to it's ranking in a database. That code is working. I'm trying to write code that prevents a user from liking a quote multiple times but that code isn't working. You can see the site at this link Here is what I have so far. Can anyone tell me what I'm doing wrong?