hello guys
I have an Image on my page and I want to count how many times the image has been clicked on and show the number beside the image. Also I dont want the number to reset everytime the page is reloded.
How can I do that
Thank you for help
Printable View
hello guys
I have an Image on my page and I want to count how many times the image has been clicked on and show the number beside the image. Also I dont want the number to reset everytime the page is reloded.
How can I do that
Thank you for help
You have to use AJAX for that purpose. And you also need to save your current counter somewhere on the server, for example in the database.
Just send AJAX request only after clicking on the image and update the number of clicking.
A simple image counter can be made without Ajax like on this page (see the source). The imagecounter.php file remove, on each call, the javascript file imagecounter.js with the new value and reload it to display this one...
A single difficulty, it would be advisable to preload the sound !Code:<?php
// imagecounter.php file
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type:text/javascript;charset=utf-8");
$cnt=file_get_contents("imagecounter.js");
$nmb=preg_replace("`[^\d]+`",'',$cnt);
$nmb++;
$fch=fopen("imagecounter.js","w+");
$ncn='var count='.$nmb.';'.chr(13).chr(10);
$ncn.='document.getElementById("counter").innerHTML=count;';
// remove the js file
fwrite($fch,$ncn);
// return the new script
echo $ncn;
?>