Jump to content
Mellow

Button counter save

Recommended Posts

Hello, so i have this button that counts every time you tap it, but whenever you refresh or closes the tab it resests.

I've been trying for so long to find a way for it to save your progress, but nothing seems to work. Can u help me?

 

<body>
<div style=" text-align: center;" 
<h1>Hello my friend if you love chocobos, then you are the right place</h1>
<br>
   <script type="text/javascript">
   var Love = 0;
   function onClick() {
       Love += 1;
       document.getElementById("Love").innerHTML = Love;
   };
   </script>
   <input type="image" src="heart.png" onclick="onClick ()" width="62" height="62" />
   <p>Love given: <a id="Love">0</a></p>

</div> 

Edited by Mellow

Share this post


Link to post
Share on other sites

You require a server side database (PHP/MYSQL is one option) with minimum columns ID, counter_name, count, on every click of button, it would send ajax request to a php page, this php page will update by incrementing the number in count column count=count+1 to a specific ID, afterwards this value is retrieved with SELECT query and echo the same the php page, this is returned back to the page where click event occurred, where it would update the value within element with id 'Love'.

Share this post


Link to post
Share on other sites

Unclear on the request, so for clarification:

Does the button count need to be stored on the server for counts from more than one user?

Or, can the button count be saved (and/or reset) with local storage on the users single computer?

Share this post


Link to post
Share on other sites

The count is stored in a single row with a id ref, and probably a count identifier if you have more than one counter on a database, local storage stores data specific to the user visiting a site, it will never able to register and update other users triggering a count event.

Every time someone clicks the button or whatever, it goes to the database and increments the current value by 1, the current total result is then retrieved and shown on the website.

Share this post


Link to post
Share on other sites
On 7/18/2018 at 5:14 PM, dsonesuk said:

The count is stored in a single row with a id ref, and probably a count identifier if you have more than one counter on a database, local storage stores data specific to the user visiting a site, it will never able to register and update other users triggering a count event.

Every time someone clicks the button or whatever, it goes to the database and increments the current value by 1, the current total result is then retrieved and shown on the website.

Sorry, I understood your suggestion.  I have used it in the past as well.

What I was asking the OP for was clarification as to how the count request was to be used.  Server (your solution) or client side (via local storage).

Share this post


Link to post
Share on other sites

Local storage is not practical as it would not store anyone else's click count but your own, it would not sync with anyone's else's clicks or the current total of clicks.

Share this post


Link to post
Share on other sites
8 hours ago, dsonesuk said:

Local storage is not practical as it would not store anyone else's click count but your own, it would not sync with anyone's else's clicks or the current total of clicks.

Sorry, but I did not see any requirement from OP that the code requested needed to be on a server,
hence the initial question.  Obviously the OP has his/her answer or has given up. 
Either way, no need to argue about requirements when either could work if there were more request clarification.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×