2

I am working on a site for a friend who is releasing an energy drink. He has asked for some stuff that is a little advanced for me, and I told him I could try but he may need to get someone else. The page (so far) is here

The button there counts fine, but when I reload the page the variable is reset to 0. I keep thinking that I can use php to store that variable but I'm just not sure how and can't figure it out as I am new.

I'm helping him build this, but it's more for me to learn than anything. Any help on how to store the number of time that button is clicked would be appreciated. I need the count to be stored permanently, so when clicked by anybody anywhere it shows the total count...it acts like a like button so to speak.

<script type ="text/javascript">

var x = 0;
function count()
{

x += 1;
document.getElementById( "counting" ).value = x;

}
</script>
</head>
<body>
<input id = "counting" type = "text" / class="counter"><br>people are excited about<br>                      <strongb>EXP Energy!</strong></h1><p>&nbsp</p>

Show EXP Energy some love and let the world know that you're excited about its release.

<p></center>
<p>&nbsp</p><a class="push_button" href="#" input onclick = "count()"/>I'm  Excited  Too!</a>
</p>    

3 Answers3

1

the count function you are doing is a JS function and so it is a client side.You need to use JSP and a database(mysql is a opensource) to store the count.When the button is placed then call a function that counts and using ajax store it in database.While displaying use jsp so that it can be communicated with the database and you can get the stored value.Suppose you do not want to the count value permanently then I suggest you to go for session. So in session as long as the session is not expired or user doesnt close the browser you will get the count value. In JSp use session.setAttribute() to store the count in session and use session.getAttribute() to get the count value

SpringLearner
  • 13,195
  • 20
  • 69
  • 111
0

Since the question is tagged as PHP. Coming straight to the point, On every click you will have to fire an ajax request to a php script say saveCount.php and send current count as parameter(POST or GET). In saveCount.php connect to a database (e.g. mysql) and store the count. Every time a user loads a page, fetch the count and display on your page.

  1. Ajax Jquery :- Ajax Jquery
  2. Ajax MDN :- Ajax MDN

Thanks.

vaibhavmande
  • 1,014
  • 9
  • 15
0

As suggested above you can use JSP and persist the count in DB (MySQL opensource) and retrive on every click or on page load, but it may not suit you, if you are working only with HTML. But i would suggest you to be clear with your requirement also do let us know what the count does. On click on 'I am Excited Too' JS is called which increments the count by 1, and it is on client side. Also if you are storing in the session then the count will be different for every session. There many other ways you can handle it provided you should be clear with the requirement.

Samy
  • 1,929
  • 2
  • 14
  • 28