xProteuSx Posted December 18, 2012 Share Posted December 18, 2012 I would like to create a hit counter that is linked to a mysql database. I would like to do something like this: <img src="button.jpg" onclick="addhit.php?id=12" /> where addhit.php would have a script that adds a hit to item with id number 12. I can do all of the php no problem, but I just don't know how to create a javascript onclick event that will execute something like that. Please keep in mind that I would like addhit.php to run in the background. I do not wish for it to load any content. I have never done anything like this, so your input and suggestions are much appreciated. Cheers. Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 19, 2012 Share Posted December 19, 2012 I hate onclick attributes. I find it much cleaner to put it all into JS. I would suggest using jQuery. This way, you can do it all very easily. $.click() will trigger when you click the image. $.post() will send data to the PHP page to trigger the counting script. Note that you should use POST instead of GET so clicks ain't taken up by going to a URL. Spiders, people refreshing, hotlinking, etc. It will make your count useless. If you don't care, then you may as well just use fake large numbers to make it look good. POST will make it much less abused by things, usually accidentally by spiders. You should also check that only one click counts per certain time period, like 24 hours so people using it multiple times a day won't count as multiple people. You could even make it unique, (only one ever instead of one a day) depending on what you're doing. // Let page load $(document).ready(function() { // Click trigger for image $("#myBtn").click(function() { // Post, data, return $.post("addhit.php", { id: 12 }, function(data) { // Returned data alert(data); }); }); }); Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted December 23, 2012 Share Posted December 23, 2012 If you do not want to load anything but just want a background process you may try this. <html> <script tye="text/javascrip"> function fnc_LoadAjax(str) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","HitCounter_action.php?q="+str,true); xmlhttp.send(); } </script> <body> <input type="submit" value="AddCounter" onclick="fnc_LoadAjax('12');"> <body> </html> where HitCounter_action.php has the following code <?php $Id = ''; $Id = $_REQUEST['q']; $con = mysql_connect("localhost","root",""); if($con) { mysql_select_db("test",$con); $sql = "select HitCount from tblhitcounter where ID = '".$Id."';"; $row = mysql_query($sql,$con); if(mysql_num_rows($row) > 0) {$res = mysql_fetch_array($row); $HitCount = $res['HitCount']; } $sql1 = "update tblhitcounter set HitCount =('".$HitCount."' + '1') where ID = '".$Id ."';";; mysql_query($sql1,$con); } ?> Let me know if this solves your purpose. Quote Link to comment Share on other sites More sharing options...
P5system Posted February 7, 2013 Share Posted February 7, 2013 Use ajax to fullfill the requirement. onclick with page name will not fullfill your requirement. Create an ajax function and call hit.php in that function and do the calculations overthere Quote Link to comment 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.