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
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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.