Jump to content

How To Link Onclick With Php Function


xProteuSx

Recommended Posts

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.

Link to comment
Share on other sites

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);
       });
   });
});

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

  • 1 month later...
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.