Jump to content

Recommended Posts

I'm working on a five star voting system for a website and I have very limited knowledge of cookies and javascript for that matter. I'm attempting to limit users to only one vote per instance so I'm guessing I'll need some sort of count and a cookie tracking system that could delete after a couple days. I'm not necessarily worried about people clearing cache as the votes aren't incredibly important but I definitely don't want the ability to keep voting via click. Here is what I've accomplished so far:

 

<script>
$(document).ready(function() {

$('.rate_widget').each(function(i) {
var widget = this;
var out_data = {
widget_id : $(widget).attr('id'),
fetch: 1
};
$.post(
'ratings.php',
out_data,
function(INFO) {
$(widget).data( 'fsr', INFO );
set_votes(widget);
},
'json'
);
});

$('.ratings_stars').hover(
function() {
$(this).prevAll().andSelf().addClass('ratings_over');
$(this).nextAll().removeClass('ratings_vote');
},
function() {
$(this).prevAll().andSelf().removeClass('ratings_over');
// can't use 'this' because it wont contain the updated data
set_votes($(this).parent());
}
);

$('.ratings_stars').bind('click', function() {
var star = this;
var widget = $(this).parent();

var clicked_data = {
clicked_on : $(star).attr('class'),
widget_id : $(star).parent().attr('id')
};
$.post(
'ratings.php',
clicked_data,
function(INFO) {
widget.data( 'fsr', INFO );
set_votes(widget);
},
'json'
);
});

});
function set_votes(widget) {
var avg = $(widget).data('fsr').whole_avg;
var votes = $(widget).data('fsr').number_votes;
var exact = $(widget).data('fsr').dec_avg;

window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes);

$(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
$(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote');
$(widget).find('.total_votes').text( votes + ' votes recorded (' + exact + ' rating)' );
}
</script>

Edited by daebat
Link to comment
https://forums.phpfreaks.com/topic/271999-cookie-help/
Share on other sites

Make the stars a form with 5 links (the stars). Post this to a PHP page which will verify the limit and whatnot. When someone votes, add it to their $_SESSION via PHP. In the HTML, make sure you don't allow voting if it's in the session that they voted for that one already.

 

This will allow them to try and vote again when they come back to it, but you could block that by joining it with the query you use to get the items they're voting on, then you could see if they voted for it and block the ones they did. If you don't already use a query for all of that, it would be an extra query unless you can put it on to another one. In that case, I would stick to the session and just disallow it after they try to vote twice.

Link to comment
https://forums.phpfreaks.com/topic/271999-cookie-help/#findComment-1399397
Share on other sites

Make the stars a form with 5 links (the stars). Post this to a PHP page which will verify the limit and whatnot. When someone votes, add it to their $_SESSION via PHP. In the HTML, make sure you don't allow voting if it's in the session that they voted for that one already.

 

This will allow them to try and vote again when they come back to it, but you could block that by joining it with the query you use to get the items they're voting on, then you could see if they voted for it and block the ones they did. If you don't already use a query for all of that, it would be an extra query unless you can put it on to another one. In that case, I would stick to the session and just disallow it after they try to vote twice.

 

Doesn't session expire really quickly though?

Link to comment
https://forums.phpfreaks.com/topic/271999-cookie-help/#findComment-1399430
Share on other sites

Except the session cookie is deleted whenever the client closes their browser, which in turn means that said session is no longer associated with that user.

I'd advice you to use setcookie () instead, it'll last until the client manually cleans the cookies or your code specifically deletes it.

Link to comment
https://forums.phpfreaks.com/topic/271999-cookie-help/#findComment-1399504
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.