I am working on a site that allows visitors to download various documents using this code:
<input type="button" class="downloadedFile" value="Download File" onclick="window.location.href=\'documents/' . $docsRow['docFileName'] . '\'" />
$docsRow[‘docFileName’] is the path of each document, which is retrieved from a MySQL database.
The bigger picture is that these documents are in a jQuery accordion, with each accordion pane being a class with one or more documents – like such:
<div id="accordion">
<h3><a href="#">LEAD5220 - Leader as Change Agent</a></h3>
<div>
<p class="className">Leader as Change Agent</p>
<p class="classNumber">LEAD5220</p>
<hr class="classSeparator" />
<table width="95%">
<tr>
<td><strong>Document Name:</strong> Generic Annotation Format</td>
<td width="100px" align="right"><input type="button" class="downloadedFile" value="Download File" onclick="window.location.href='documents/Generic Annotation Format.docx'" /></td>
</tr>
<tr>
<td colspan="2"><strong>Description:</strong> This document can be used as a template for your annotations in the annotated bibliography forums.</td>
</tr>
</table>
<hr class="classSeparator" />
...
My client wants to be able to count the number of times each document has been downloaded per class.
I known I can detect the button click via javascript, and as far as the “per class” part, I can use jQuery to detect in which class table the button clicked, but what I don’t understand is 1) how to get that info into PHP/MySQL code to record to the database, and 2) whether I can only detect the button click, or whether I can detect if the user actually downloaded the file to their computer (these are PDF, DOC, XLS files).
Thanks for anyone’s help!
There are 2 options to count the number of downloads. One is to link to a php page that records every hit and than redirects to the actual file. Something like:
In the html the link can be something like:
The second approach, which I personally prefer, is to record the number of hits with ajax. When the user clicks the link to download do something like:
this way you send the data to be recorded, and the user stays in the same page.
If you need more clearifications let me know.