Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQuery投票系统_Php_Javascript_Jquery_Voting System - Fatal编程技术网

Php jQuery投票系统

Php jQuery投票系统,php,javascript,jquery,voting-system,Php,Javascript,Jquery,Voting System,所以我在做一个投票系统,基本上是一个竖起拇指和竖起拇指的投票系统。我在MySQL中使用CakePHP和jQuery,但希望确保前端是正确的,这是最好的方法 我希望用户能够更改他们的投票,因此,使用jQuery是最好、最有效的方法吗?对于类操作,我对jQuery非常熟悉 ID字段将是用户将投票的照片的唯一ID。当然,这只是一个测试,不会成为生产中的最终产品。页面上会有多张照片,用户会对每张照片进行上下投票 这是代码 <?php echo $javascript->link('jquer

所以我在做一个投票系统,基本上是一个竖起拇指和竖起拇指的投票系统。我在MySQL中使用CakePHP和jQuery,但希望确保前端是正确的,这是最好的方法

我希望用户能够更改他们的投票,因此,使用jQuery是最好、最有效的方法吗?对于类操作,我对jQuery非常熟悉

ID字段将是用户将投票的照片的唯一ID。当然,这只是一个测试,不会成为生产中的最终产品。页面上会有多张照片,用户会对每张照片进行上下投票

这是代码

<?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>

$(文档).ready(函数(){
$('.vote')。单击(函数(){
if($(this).hasClass(“当前”)){
警告(“您已经投票支持此选项!”);
返回;
}
var parentId=$(this.parent(“div”).attr(“id”);
if($(this).hasClass(“up”)){
//在这里执行后端查询和检查
警惕(“投票赞成!”);
$(此).toggleClass(“当前”);
if($(“#”+parentId+“-down”).hasClass(“当前”)){
$(“#”+parentId+“-down”).toggleClass(“当前”);
}
}
else if($(this.hasClass(“down”)){
//在这里执行后端查询和检查
警惕(“投票否决!”);
$(此).toggleClass(“当前”);
if($(“#”+parentId+“-up”).hasClass(“当前”)){
$(“#”+parentId+“-up”).toggleClass(“当前”);
}
}
});
});
+ | -

是的,jQuery是解决这一问题的最佳解决方案,但我不确定您是否可以进一步优化代码。

您可以这样做:

<script type="text/javascript">
    $(document).ready(function() {
        $('.vote').click(function () {
            if ($(this).hasClass("current")) {
                alert("You have already voted for this option!");
            } else {
                var parentId = $(this).parent("div").attr("id");
                var error = false;

                if ($(this).hasClass("up")) {
                    //Do backend query and checking here (SET: error)
                    alert("Voted Up!");
                }
                else if($(this).hasClass("down")) {
                    //Do backend query and checking here (SET: error)
                    alert("Voted Down!");
                }
                //removes all the votes 
                $(this).parent("div").children().removeClass("current");

                if(!error) {
                    $(this).addClass("current");
                } else {
                    alert("There was an error");
                }
            }
            return false;
        });
    });
</script>
<div id="1234">
    <a class="vote up" href="#">+</a> | <a class="vote down" href="#">-</a>
</div>

$(文档).ready(函数(){
$('.vote')。单击(函数(){
if($(this).hasClass(“当前”)){
警告(“您已经投票支持此选项!”);
}否则{
var parentId=$(this.parent(“div”).attr(“id”);
var错误=错误;
if($(this).hasClass(“up”)){
//在此处执行后端查询和检查(设置:错误)
警惕(“投票赞成!”);
}
else if($(this.hasClass(“down”)){
//在此处执行后端查询和检查(设置:错误)
警惕(“投票否决!”);
}
//删除所有投票
$(this.parent(“div”).children().removeClass(“current”);
如果(!错误){
$(此).addClass(“当前”);
}否则{
警报(“出现错误”);
}
}
返回false;
});
});
| 
它消除了对额外html id的需要,并且您不需要使用折叠代码来添加当前类。我不确定哪种方法更快,但我认为这种方法可以更好地维护代码。

您可以使用这种方法:
披露:我是开发人员。

那么问题是什么呢?在JavaScript中,id必须以字母开头,并且只能包含字母、数字或下划线。@偷偷摸摸:你在说什么?那么为什么代码按原样工作呢@SimpleCoder:我的问题是“这是实现我想要实现的目标的最佳方式吗?”。jQuery中是否有我没有使用过的函数,我可以使用这些函数使代码变得更简单,并去掉一些if语句。这在我看来是相当精简的。我不知道如何删除一些if语句。IMO Jquery是做你正在做的事情的最佳工具。太棒了!看起来不错,我明天会测试,让你知道$(this).parent(“div”).children().removeClass(“当前”);这是我想做的事,但不确定该走哪条路!没问题!您可能应该移动$(this.parent(“div”).children().removeClass(“current”);进入if(!error)条件。因此,如果服务器端出现问题,您似乎不会删除对用户的投票。祝你好运:允许使用自定义算法的DDoS?@jostster,我想你说的是投票结果。如果是的话,是的。您只需修改PHP类。