i am trying to set up a download counter for files. My plan is: When a user clicks on the download button, the id of that file is sent to the download controller, the download controller updates the download column by 1 with its unique id, and then user is redirected back to the view, and download starts. I have found a way to send the id of a particular file to the download controller when the download button is clicked. I used a form with hidden fields in the view to send the id. I checked online, and i saw many download counters but most of them update textfiles. I want a column in the database to be updated, not text files. Here is my download form view:
<form id="form" action="<?php echo base_url();?>Download" method="post">
<input type="hidden" placeholder="ID" name="id" value ="<?php echo $res->id; ?>" class="wow fadeInUp">
<input type="hidden" placeholder="ID" name="url" value ="<?php echo $res->url; ?> class="wow fadeInUp">
<input type="submit" value="DOWNLOAD" id="download" class="wow fadeInUp">
</form>
This view sends id and other data to the download controller, here is my download controller:
function index()
{
$id = $this->input->post("id");
$url = $this->input->post("url");
$this->home_model->update_downloads_column($id);
redirect("Music/".$url);
}
i need a way forward. Right now, the downloads column on the database has been updated, how do i now make download of that file start?
Why not use ajax to pass value and the anchor tag in html that has download attribute?
check that here :
Code:
<a href="/pathtofile/file.txt" id="download" download>
And then just do something like :
<script>
$(document).ready(function(){
$("#download").on( "click",function () {
var id = $('input[name=id]').val();
var url = $('input[name=url]').val();
$.ajax({
type: "POST",
url: <?php echo base_url();?>+"Download",
data: {id: id, url: url},
success: function(){
//if success do something
}
});
});
})
</script>
So when you click anchor, download will start and ajax will pass id and url to controller.