Live count-up based on button clicks

Hi there,

I am wanting to create a live count-up that counts the number of times a button with a class is clicked. My website has various buttons across the site with the same class.

What I am wanting to do is to display a number at the top of my pages which counts up each time a button is clicked on my site.

What would be the best way to do this and does anyone know of any snippets/code that already do this?

I’m guessing the count would need to be stored in a text file and use AJAX?

Any suggestions would be great.


You could store it in a text file, which might be marginally easier than storing in a database if you’re not familiar with databases. Ajax would be a good thing - use it to trap the button press, call some code on the server to update the stored number, and then update the display to show the new value (as others might click it at the same time).

Plenty of examples of Ajax in the last week or two on this section of the board, also in the JavaScript section. There’s another thread on here too showing how to write in a text file. Have a go at it and post some code if you have trouble.

OMG NO. Only if you want to get into real trouble and do not need any valid number over time. Plain text files are a mess when it comes to concurrent writes. If you want results you can rely on, use a database with ACID compatibility, at least SQLite, it’s build in and you get a counter with a few lines of code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.