热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

单击时的SQL增量(使用ajax)-SQLincrementonclick(usingajax)

Imworkingonasimplebuttonthatwillallowstoincrementalineinmytable.ThankstoAJAX,whe

I'm working on a simple button that will allows to increment a line in my table. Thanks to AJAX, when the button +1 is clicked, it is replaced by a -1 button. But the problem occurs when I click once on +1, then -1 and a second time on +1.

我正在研究一个简单的按钮,它可以增加表格中的一行。感谢AJAX,当点击按钮+1时,它被-1按钮取代。但是当我在+1上点击一次然后在+1上再次点击-1时会出现问题。

This is what i'm having :

这就是我所拥有的:

vote = 0
click on "+1"
 vote = 1
click on "-1"
 vote = 0
click on "+1"
 vote = 2
click on "-1"
 vote = 0
click on "+1"
 vote = 3

So as you can see, vote should be egal to 1...

所以你可以看到,投票应该是1 ...

Here is my code :

这是我的代码:


 
 

AJAX :

        $('button').on("click", function test() {
        $(this).off("click");

        var ID = $(this).attr("id");

        if($(this).hasClass('button2')) {
            $.ajax({
                type: "POST",
                url: "add.php",
                data: {"ID_oeuvre": ID},
                success: function(html){
                    $("button#"+ID).removeClass("button2").addClass("button").html("-1");
                    $("button#"+ID).on("click", test);
                }
            });
        } else {
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: {"ID_oeuvre": ID},
                success: function(html){
                    $("button#"+ID).removeClass("button").addClass("button2").html("+1");
                    $("button#"+ID).on("click", test);
                }
            });
            $(this).on("click", test);
        };
    });

ADD.PHP

    $requete = $bdd->prepare('UPDATE vote SET nb_vote = nb_vote + 1 WHERE ID_member = :ID_member');
    $requete->execute(array('ID_member' => $member_ID));
    $requete->closeCursor();

DELETE.PHP is the same as above, just replace nb_vote + 1 with nb_vote - 1

DELETE.PHP与上面相同,只需用nb_vote - 1替换nb_vote + 1

This problem only happens when I don't reload the page. If I reload the page after each click on the button, there is no problem. So I think that the problem is due to the fact that since there is no page reloading, nb_vote kept the old value but that doesn't explain why it goes back to 0 after that...

只有在我不重新加载页面时才会出现此问题。如果我在每次单击按钮后重新加载页面,则没有问题。所以我认为问题是由于没有页面重新加载这一事实,nb_vote保留了旧的值,但这并不能解释为什么它会在之后回到0 ...

Thank you very much for your help.

非常感谢您的帮助。

Regards.

1 个解决方案

#1


2  

You have extra event listener binded by

你有额外的事件监听器绑定

$(this).on("click", test);

it's in the else clause. If you open network page in firebug, you should see duplicate requests.

它在else子句中。如果您在firebug中打开网页,您应该会看到重复的请求。


推荐阅读
author-avatar
于俊毅智文建志
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有