Link to home
Start Free TrialLog in
Avatar of jonathan-dunstans
jonathan-dunstansFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Jquery and PHP

I am working on an update of our recruitment site as am looking into using a dropdown to allow the user to access the content. I have gotten the drop down working fine I just can't work out a way to get the usage stats that we currently have from when users view an advert.

How can I get a one added to the correct entry when a user clicks through? I am using jquery and php with a mysql database.

For example a user clicks on our top job ad the content unfurls below the job title and a click is registered in our database to show that someone has viewed it.

For all I know this could be done solely with javascript but my jscript knowledge is in its infancy.
<script src=""></script>
$(document).ready(function() {
$('a.linka').click(function() {
        var id = $(this).attr('id');
     $('#linkbox' + id).toggle(500);
        // alert(id);
     return false;
<div class="linkbar"><a href="'.$reclist['jobID'].'" id="'.$reclist['jobID'].'" class="linka" name="'.$reclist['jobID'].'" style="color:#BA3F3F">'.$reclist['jobTitle'].'</a></div>
if (!empty($reclist['logo'])) { echo '<img src="mysite/images/recruitment/logos/'.$reclist['logo'].'"'; if($width > 150){ echo ' width="150">'; }else{ echo 'height="70">'; }} echo'
<td colspan="4">
<div id="linkbox'.$reclist['jobID'].'" class="linkbox" style="display:none">

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

To make this work, you need to call a script on the server.  The script will update the data base to increment the counter.  One popular method would be to call a script that displays an image.  The script would first do the update, then pass the image through.  So instead of an image tag that looked like this:

<img src="my.jpg" />

You might have one that looked like this:

<img src="update_script.php?jobID=1234&img=my.jpg" />

The use of jQuery may actually complicate this a bit - you would not want to pre-load the image into a hidden DIV, you would want to load it only after there was a click on the link to view the job.

You could add in the javascript click function a jQuery ajax call to a php script on your server, pass that script the job number and all that script needs to do is increment a field in your database tracking the clicks and return back an xml document stating success or failure of the query.

Avatar of hielo
Flag of Wallis and Futuna image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonathan-dunstans


This almost gets me there but I need a way for it to only record opens, it currently adds one if I am closing the area as well. So is there some kind of function for only doing on show?
Open your page via the browser, Look at the source code, then copy it and paste it here.
The following did the business. Thanks for getting me the first step of the way.
  $('a.linka').toggle(function() {
    var id = $(this).attr('id');
    $('#linkbox' + id).show(500);
    $.post('clickrecorder.php', {"productID": encodeURIComponent(id)} );
  }, function() {
    var id = $(this).attr('id');
    $('#linkbox' + id).hide(500);

Open in new window

Needed a bit of alteration but worked very nicely. Thanks.