View Full Version : How to call some PHP, when I click on an image?


Waldo
21-04-2009, 14:42
On my site, when the user clicks on an image (image is also a link to a downloadable file), I want to run some PHP code.

Best I've found so far, is using onclick inside the image tags, to call some JavaScript. Would that work? Is there a better way?

Thanks.

adaline
21-04-2009, 14:56
You could make the image link to a php script, rather than the download. The php script will log a hit (assuming thats what you want to do) as well as starting the download of the file.

Waldo
21-04-2009, 15:16
Yeah that could work thanks adaline.

I don't want the php script to show on the screen though. Just want it so they click on the image, the download starts, and I can run a little PHP to log the download in to a database. I don't want it to open a seperate page.

Would your method work for that?

Hmm, or I guess I could have it open a seperate page, and say thanks for downloading, or something.

probedb
21-04-2009, 15:56
If it's a file they're downloading when they click the image then just make a PHP script that does your logging and passes the file for download through to the user. You just set the headers correctly in the PHP and the browser will see it as whatever filetype it should be.

Ghozer
21-04-2009, 15:58
yeah, just href the image to dowload.php, and have that process ?imagename= or something maybe.

if this script doesn't echo anything out, just logs the hit and starts the download using header("Location: www.whatever.com/file.zip"); for example at the end of the script.