-1

My question to save time:

What is the simplest way to count clicks of a button, save that value to a Wordpress MySQL db and retrieve that updated total number into a div with Ajax, so the page doesn't need to up reloaded to see it?

I'm just learning how to dive into more UX glory with added mysql db support and Ajax updates with jQuery as the driving force.

My knowledge of working with databases in general is... green to say the least.

I have another question going, but I thought I'd open this up as a more general question. I see lots of tutorials online, but not a lot of elegant, minimal code like I know I've learnt here on SO already.

OP is here: Counting clicks with jQuery and displaying with Ajax

4

1 回答 1

1

您可以绑定到按钮的单击事件,然后告诉您的服务器端逻辑增加数据库中的计数,如下所示:

$("#YourButton").click(function() {
    $.ajax({
        url: '/PathToServerSide',
        type: 'POST',
        data: {increment: true},
        success: function(data) { 
            alert('Server click count updated!') 
        }
    });
}

注意:永远不要相信客户端告诉服务器端计数值是什么,而是发送一个命令(increment)告诉服务器端逻辑增加数据库中的值。

要显示点击量,请使用data从 AJAXsuccess回调和 jQuery.html()函数返回的值,如下所示:

$('#YourCounter').html('The button has been clicked ' + data + ' times`);
于 2013-08-28T02:22:39.843 回答