Jump to content

Jquery click counter.


TottoBennington

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/256691-jquery-click-counter/
Share on other sites

  • 2 weeks later...

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

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.