javascript - 如何根据 php 中的按钮操作传递点击次数

标签 javascript php onclick file-get-contents file-put-contents

我们正在开发网站。我们有按钮。在按钮操作中,我们放置了 PDF 文件下载。工作正常。但我们需要如何计算从我的网站下载我的 PDF 文件。

我们这样试过,但我们没有得到。

<?php
//$Down=$_GET['Down'];
$a="hello";
$i=0;
?>
<script type="text/javascript">
    var i=0;
    function submitForm()
    {
        <?php
        $i = @file_get_contents('count.txt'); // read the hit count from file
        echo $i; //  display the hit count
        $i++; // increment the hit count by 1
        @file_put_contents('count.txt', $i); // store the new hit count
        ?>  
    }
</script>
<html>
<head>
</head>

<body>
    <input type="button" onclick="submitForm()" value="submit 1" />
</body>
</html>

我们什么都没有。我们是 PHP 的新手,请指导我们。

最佳答案

好吧,您可以下载 PDF 并在表单提交时计数:

<?php
$i=0;
$i = (int) @file_get_contents('count.txt'); // read the hit count from file
if(!empty($_POST)) {
    $i++; // increment the hit count by 1
    @file_put_contents('count.txt', $i); // store the new hit count
    // Download
    $file = 'filename.pdf';
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
}

现在使用 echo $i; 来显示下载数量。

在 html 中,表单应该是:

<form method="post" action="">
    <input type="submit" name="download" value="Download">
</form>

关于javascript - 如何根据 php 中的按钮操作传递点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801246/

相关文章:

javascript - 从 C# 中的代码隐藏将 JavaScript 注入(inject)到链接按钮

javascript - 如果字符串放置在 iframe 之后,则不会显示使用引导列表

javascript - 使用 AJAX 响应提交数据并修改文档

PHP_AUTH_USER 未设置?

jquery - 当我完成 onclick show div 时隐藏按钮

javascript - 在javascript中用逗号分隔的字符串分割值

javascript - 是否可以禁用整个主体(包括内部 iFrame)的填写付款表格?

php - session 变量不起作用? (PHP)

javascript - forEach 代码不起作用,但两个不同的 forEach 函数中的相同代码却起作用,为什么?

java - div 类包含 onclick 元素如何在 selenium 中处理它