Hello!

Made on jquery the so-called counter of clicks on the picture and hits.

Here is the javascript code:

jQuery(document).ready(function($){ $('.click').click(function(){ var id = $('input[name=id]').val() $.get('response.php', {'click':true, 'id':id}); }); $('.hits').load(function(){ var id = $('input[name=id]').val() $.get('response.php', {'hits':true, 'id':id}); }); }); 

Pictures are displayed as:

 <div class="show"> <a href="http://#" class="click" title="summer" target="_blank"> <img src="http://.../logo-girl-summer.png" class="hits" alt="summer"> <input type="hidden" name="id" value="4"> </a> </div> 

Hidden field I pass the id of the picture.

Well, the whole thing when you click or show is transmitted in response.php and processed there.

Everything works, but I discovered a bug that if 2 pictures on one page, then only TOP ... reacts to clicks and impressions. the hidden field <input type="hidden" name="id" value="4"> transmits the id of the top picture when you click on any of them.
How to make a click on your id?

    1 answer 1

    Try

     $(this).find('input[name=id]').val() 
    • Thank you For clicks it works fine. And do not tell me how to make shows? If you add $ (this) .find ('input [name = id]'). Val () to shows, it does not respond at all. And as var id = $ ('input [name = id]'). Val (), adds 2 shows to one id instead of 1 show to 2 id. - hardwaremaster
    • Something of event type load (). But it is better to catch shows directly by the server. <img src = "http: //.../logo-girl-summer.png" class = "hits" alt = "summer"> the show_image.php file receives the name of the picture via mod_reqrite. Returns the header header ('Content-Type: image / png '); print (file_get_contents ('NAME OF HISTORY')) - knes
    • Well, if I’ve already stopped at jquery, I’m catching it like this: $ ('. Hits'). Load (function () {var id = $ (' input [name = id] '). Val () $ .get (' response.php ', {' hits': true, 'id': id});}); Is there a way to define your own id, to which you specified in <input type = "hidden" name = "id" value = "4">? - hardwaremaster
    • Yes. var arr_of_ids = $ ("input [name = id]"). map (function () {return $ (this) .val ();}). get (); then either glue through arr_of_ids.join (','); and send the line 123,4,5 Or send an array. - knes