So I am making a voting system, basically a Thumbs Up & Thumbs Down voting system. I am using CakePHP and jQuery with MySQL but want to make sure the front end is correct and this is the best way to do it.
I want the user to be able to change their vote, so utilizing jQuery is this the best and most efficient way? I'm fairly novice with jQuery when it comes to class manipulation.
the ID field is going to be the unique id of the photo the users will be voting on. This of course is just a test and this is not going to be the final product in production. There will be multiple photos on the page and the user votes Up or Down for each of them.
Here is the code.
<?php
echo $javascript->link('jquery/jquery-1.4.2.min',false);
?>
<script type="text/javascript">
$(document).ready(function() {
$('.vote').click(function () {
if ($(this).hasClass("current")) {
alert("You have already voted for this option!");
return;
}
var parentId = $(this).parent("div").attr("id");
if ($(this).hasClass("up")) {
//Do backend query and checking here
alert("Voted Up!");
$(this).toggleClass("current");
if ($("#" + parentId + "-down").hasClass("current")) {
$("#" + parentId + "-down").toggleClass("current");
}
}
else if($(this).hasClass("down")) {
//Do backend query and checking here
alert("Voted Down!");
$(this).toggleClass("current");
if ($("#" + parentId + "-up").hasClass("current")) {
$("#" + parentId + "-up").toggleClass("current");
}
}
});
});
</script>
<div id="1234">
<a id="1234-up" class="vote up" >+</a> | <a id="1234-down" class="vote down" >-</a>
</div>