TottoBennington Posted February 8, 2012 Share Posted February 8, 2012 Hi people, I want to do a system in which when the user clicks, add + 1. And when i click to refresh the browser does not erase the values and continue to accumulate. Well the counter works, but when i refresh it returns to 0 . WHAT CAN I DO? <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <input type='submit' value='Like' onclick='' id='aa' /> <p style='clear: left;'> <span>0</span> like this. </p> <script> $("#aa").click(function () { var n = parseInt($("span").text(), 10); var a=0; n = n + 1; $("span").text( a = parseInt(a) + parseInt(n)); }); </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256691-jquery-click-counter/ Share on other sites More sharing options...
mdaudali Posted February 18, 2012 Share Posted February 18, 2012 It would be much better to use ajax to send the click data and to recieve the amount of clicks aswell. //Jquery send click data $.post('send click.php',key:'value pair for post', your function if successful an alert or something to e.g. reward them) $.post('click amount.php',key:'value pair for post', function(data){document.write(data);}) //the data parameter is the html source code of your php page //the post data is optional and so is the function //however the function is required in the second example Code for send click.php mysql_connect($host,$username,$password) or die('failure'); mysql_select_db($database) or die('failure'); mysql_query('UPDATE clicks SET Clicks = Clicks + 1') or die('failure'); //if you had one table for all your posts then you need the where clause echo 'success'; //for function in jquery ajax - optional Code for click amount.php mysql_connect($host,$username,$password) or die('Undefined'); mysql_select_db($database) or die('Undefined'); $result = mysql_query('SELECT Clicks FROM clicks') or die('Undefined'); $row = mysql_fetch_array($result) //fetches result from query //if you had one table for all your posts then you need the where clause echo $row['ClicksPU']; //for function in jquery ajax - required Quote Link to comment https://forums.phpfreaks.com/topic/256691-jquery-click-counter/#findComment-1318517 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.