使用下载计数器在AJAX中下载文件

时间:2015-09-15 10:26:02

标签: php ajax

我在AJAX中创建简单的文件下载计数器。但是在我的代码中,计数器正在使用PHP代码,它每次下载时它的值增加一个,但文件没有下载。以下是我的代码

的index.php

<script type="text/javascript">
$(function() {

$(".download_button").click(function() {         
    var test = $("#content").val();
    var dataString = 'content='+ test;  

    $.ajax({
        type: "POST",
        url: "download_counter.php",
        data: dataString,
        cache: false,
        success: function(html){      
               $("#display").after(html);             
               }
    });
   return false;
 });           
});
</script>
<a href="file_path/file.pdf" class="download_button" id="v" download>
    Download 
</a>

download_counter.php

<?php      
       //Code for counter Increment 
      //Query to Update database
?>

我对PHP代码中的数据库或下载计数器没有任何问题。我有文件下载的问题。计数器增加1但文件没有下载。

1 个答案:

答案 0 :(得分:2)

您必须在onclick事件处理程序中将return false;替换为return true;,以便执行超链接的默认事件。

请查看此答案,了解有关事件处理程序中return false;的更多信息: https://stackoverflow.com/a/11184286/3647441