-1

I've got a short question. I want to develop a little script on our intranet. The requirements are:

  1. a button, which count +1 if you click on it
  2. just logged in users can count
  3. all other users should see the counter

I'm a beginner in JS and i only know the localStorage function, but i want to save the counter on the server, so that everybody see's the same status.

Thats what I got, but its just the localStorage, so every computer has their own status of the counter.

if (localStorage.getItem("counter")!=null) {
    counter = Number(localStorage.getItem("counter"));
    document.getElementById("counterValue").innerHTML = counter;
}

Do you know what I mean? Thanks for the help and sorry for my bad english :)

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Michi Sch
  • 3
  • 2
  • 1
    If you want to do something like this `php` or `node.js` using `express` would help you achieve a 'universal' counter – Nick Parsons Oct 05 '18 at 10:01

1 Answers1

0

You need to keep the 'counter' on the server. You should keep it in some kind of persistent storage (not memory, since the memory can be re-set):

  • simplest is file system (file)
  • DB (e.g., MySql)
  • cloud (e.g., AWS S3)

then to get it from storage and present to all users. If you need the value to be presented for all Live, then you'll have to use some solution for that like SignalR.

basdanny
  • 79
  • 1
  • 6
  • Ok thank you for the first time! I decided to store the counter on a DB. So i already got the connection to my database. What are the next steps in php to display the counter? It don't has to be live but anytime the users refreshes the page, the current status should appear. – Michi Sch Oct 08 '18 at 11:11
  • There are plenty of examples/tutorials on how to do it in the web, just google for it. e.g., https://www.w3schools.com/php/php_mysql_select.asp – basdanny Oct 09 '18 at 09:32