![]() |
How can i prevent user vote double - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: How can i prevent user vote double (/showthread.php?tid=63203) |
How can i prevent user vote double - freddy - 10-07-2015 Hello guys, thanks for some help in the past. I have some stuck, about giving voting product who came from user vote, i already success giving vote but realiaze that user cannot giving double vote, here is js Code: $(document).on("click", "[href='#atas']", function() { Controller Code: public function rating() { and this is my model Code: public function rating($data) I'm trying to prevent in controller don't know where should start ! Thanks RE: How can i prevent user vote double - InsiteFX - 10-07-2015 Assign the user_id to the post_rating then when they vote on a post set the user_id that they had voted, you can then check the user_id and post_rating to see if they had already voted for it. You would need a pivot table for this to work. id - rating_id - user_id RE: How can i prevent user vote double - freddy - 10-07-2015 (10-07-2015, 05:25 AM)InsiteFX Wrote: Assign the user_id to the post_rating then when they vote on a post set the user_id that they had voted, yes i know but hard to coding in js and controller, RE: How can i prevent user vote double - jLinux - 10-07-2015 Why dont you modify your model to add the user ID to the vote, and when it adds a new vote, look for a row with the user ID, if it exists, update it, if not, insert a new one - This would allow users to edit their votes too, which would be nice RE: How can i prevent user vote double - freddy - 10-07-2015 (10-07-2015, 11:44 AM)jLinux Wrote: Why dont you modify your model to add the user ID to the vote, and when it adds a new vote, look for a row with the user ID, if it exists, update it, if not, insert a new one - This would allow users to edit their votes too, which would be nice Thanks for your respond, i know the logic like that but how can i make it validations if users already vote and doesn't vote alert in javascript, honestly i just success inserted without checking user already vote or not, so i make the title how can i prevent user cannot vote double ! will you guide me by using my code js, controller and model if you don't mind ? RE: How can i prevent user vote double - mwhitney - 10-08-2015 You could retrieve their existing vote when the page is loaded and set a flag to indicate to the JavaScript that they have already voted. You also need to add some validation before the insert to check whether they've already voted. This is generally the perfect use for the is_unique validation rule, since you wouldn't want multiple entries in the table with the same 'id_user' value. If you want them to be able to update their existing vote and not bother with the is_unique validation, you can modify the model's rating() method to use replace() or to do a quick $this->db->where('id_user', $data['id_user'])->get() to determine whether you should use an update() or an insert(). You could also return a value from the rating() method to indicate whether an update or insert was performed (or neither), which would allow you to determine whether you need to update your li_product_post table, since you appear to be maintaining some sort of count of the votes in that table. RE: How can i prevent user vote double - PaulD - 10-08-2015 I thought the error: and success: clauses were for ajax events, such as did the ajax call work or did it time out, or no server response etc. Actual error checking of the response functionality is still done within the success: part of the ajax response. After all, the ajax request was successful, it is just that what the user tried to do was not allowed. Normally I respond with a string that starts with either 'Error: You have not provided enough information' for example or 'Success: Your vote was registered', and then you could add another response for 'Voted: You have already voted', or 'Login: Your login has expired' etc etc. I do a substr check on the response to see if it starts with Error, or in this example Voted, or Login, and assume that if it is not any of these error responses then it was a success. You can then, within your js, do whatever you need to do depending on the result. Code: var response = theResponse.substring(0,5); Hope that helps, Best wishes, Paul. RE: How can i prevent user vote double - freddy - 10-08-2015 (10-08-2015, 07:19 AM)PaulD Wrote: I thought the error: and success: clauses were for ajax events, such as did the ajax call work or did it time out, or no server response etc. Thanks paul for you guide, it works but on my way, can i ask something about your js cause i'm not too experts on it ? here is my js Code: $(document).on("click", "[href='#atas']", function() { and controller Code: public function ratingup() { my script works to vote and not allowing users double vote, just want to know more about ajax and jquery, many thanks and +1 for you |