If you want to count clicks on some link, then you have to use file or database to save the value, because PHP does not save the state.
Here I am giving an example of using a database for this:
The Ajax
$('.link').on('click',function(){
$.ajax({
url:"<?php echo 'Your url to incrementCount() method';?>",
method:"POST",
data:{},
success:function(response){
// on success do something
}
});
});
The PHP method
public function incrementCount(){
$UpdateQuery = "UPDATE link_counts SET link_count=link_count + 1 WHERE id=1";
// here 'link_counts' is table name and 'link_count' is the column name to update
$result = mysql_query($UpdateQuery );
}
The 'link_counts' table
-------+------------+
| id | link_count |
-------+------------+
Here link_count
column value is 0 by default. By this, you can update the field on the occurrence of click event of .link
class.
Alternatively, you can use user_id
to count which user clicked the .link
class. To achieve this you need to add some where
condition to the update query.
If you want to do this with a file, then change the PHP method by below code-
PHP Method for file handling
$file = 'link_counter.txt';
// default the counter value to 1
$counter = 1;
// add the previous counter value if the file exists
if (file_exists($file)) {
$counter += file_get_contents($file);
}
// write the new counter value to the file
file_put_contents($file, $counter);
Hope this helps.