realife Posted November 18, 2012 Share Posted November 18, 2012 Hello PHP... Let's say i have a button, and below that i have a number which counting the clicks on the button, for example <input type="submit" value="Add Click" /> <br> So far = 12 Clicks How can i set it that onclick it will add more to the number ( of course without refreshing the page ) ? Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/ Share on other sites More sharing options...
Manixat Posted November 18, 2012 Share Posted November 18, 2012 (edited) Assign a variable, and increase it by 1 everytime the button is clicked Edited November 18, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393357 Share on other sites More sharing options...
Andy123 Posted November 18, 2012 Share Posted November 18, 2012 My Javascript is a little rusty, but try something like this: <script type="text/javascript"> var counter = 0; function increase() { counter++; return false; } </script> <input type="submit" value="Add Click" onclick="increase()" /> Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393359 Share on other sites More sharing options...
realife Posted November 18, 2012 Author Share Posted November 18, 2012 The code is not right... I know how to count, I need to print the number everytime you clicking on the button without refreshing the page. Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393365 Share on other sites More sharing options...
Andy123 Posted November 18, 2012 Share Posted November 18, 2012 That is not exactly what you wrote. ;-) <script type="text/javascript"> var counter = 0; function increase() { counter++; document.getElementById('click_count').innerHTML = 'You have clicked ' + counter + ' times.'; return false; } </script> <input type="submit" value="Add Click" onclick="increase()" /> <div id="click_count">You have clicked 0 times.</div> Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393383 Share on other sites More sharing options...
realife Posted November 18, 2012 Author Share Posted November 18, 2012 Nice!! Thanks so much.. Now i need to figured it out how each user can click just once... Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393385 Share on other sites More sharing options...
Andy123 Posted November 18, 2012 Share Posted November 18, 2012 You are welcome. You could do like this: <script type="text/javascript"> var counter = 0; function increase() { if (counter < 1) { counter++; document.getElementById('click_count').innerHTML = 'You have clicked ' + counter + ' times.'; } else { document.getElementById('click_count').innerHTML = 'You are not allowed to click anymore.'; } return false; } </script> <input type="submit" value="Add Click" onclick="increase()" /> <div id="click_count">You have clicked 0 times.</div> Quote Link to comment https://forums.phpfreaks.com/topic/270856-please-help-with-simple-counter/#findComment-1393405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.