Count hits

If we have a button on a simple page and a user clicks on the button, then how we can come to know how many times the user has clicked on the button?

Questions by yogeshban

Showing Answers 1 - 30 of 30 Answers

garima8481

  • Jan 5th, 2009
 

Make a javascript function. Call that function each time the user click on the button.
In javascript take a global variable to count the click. eg

var count=0;
function count_click()
{
  count++;
  return count;
}

This function will return you each count of user click.


Cheers!!

amitverma

  • Feb 9th, 2009
 

Yes, above answer is corrent, the only missing thing is you need to call this "click counter" function on onClick event of mouse like given below -

<button onclick="javascript: alert(count_click());">Click Me</button>

  Was this answer useful?  Yes

amitverma

  • Feb 9th, 2009
 

echo htmlentities('Yes, above answer is corrent, the only missing thing is you need to call this "click counter" function on onClick event of mouse like given below -

<button onclick="javascript: alert(count_click());">Click Me</button>');

  Was this answer useful?  Yes

But I think if you have to count all number of hits you have to use database for that purpose. As in case of visitor counter. I mean we can have a table for that counting each time button gets clicked the counter gets incremented.

  Was this answer useful?  Yes

Yes I think garima8481's answer is correct but some extent, means I want to say
it is only on client side.  If user want to see any one days his button hit count any other day, then
we want to save some history ate server side also.
so my solution is that we can submit this user button hit count to server every time and should maintain his history.
What you say ?

  Was this answer useful?  Yes

imran7000

  • May 13th, 2009
 

You all are right. To make counter without database we can use cookies. Which will keep the count stats for 24 hours or for some defined time period.

It will happen on the client side (COOKIES) only. Server side (SESSION) or Database will not be envolved

  Was this answer useful?  Yes

stikadia

  • Aug 14th, 2009
 

You can store in one file which has content "0". Now when any body clicks that button open that file in reading mode and increment it with ++ operator. This way we can track how many times the button was clicked

  Was this answer useful?  Yes

ashishdmc4

  • Feb 8th, 2010
 

<html>
<head>
<script type="text/javascript">
var ctr=0;
function countercall()
{

ctr++;

}
function show()
{
alert(ctr);
}

</script>
</head>
<body>
<div style="position:absolute; width:300px; height:300px; background-color:#F66">

<input id="btn1" name="btn" type="button" onclick="javascript:countercall();" value="Clck Me I am A button" />
<a href="javascript:show();">show number of hits </a>
</div>

  Was this answer useful?  Yes

You can try following script for determing click count on button

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<input type="button"  value="Click" onclick="getcount()" />
<div id="div_text"></div>
</body>
</html>
<script>
var k=0;
    function getcount()
    {
        k++;
        document.getElementById("div_text").innerHTML="You have clicked "+k+" times.";
    }
</script>

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions