javascript - file.txt 中的 PHP Like 按钮计数器

标签 javascript php html css

我想重新创建几个类似的按钮,将计数保存在 file.txt 中,但这不起作用:/

<?php

function getClickCount()
{
    return (int)file_get_contents("counter.txt");
}

function incrementClickCount()
{
    $counter = getClickCount() + 1;
    file_put_contents("counter.txt", $counter);
}
?>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<script type="text/javascript">
  var clicks = 0;

  function onClick() {
    clicks = 1;
    document.getElementById("clicks").innerHTML = clicks;
  };

</script>
<button type="button" onClick="onClick()" title="Vous aimez la couverture?" class="btn"><img id="heart" src="https://trello-attachments.s3.amazonaws.com/568304b85fa72dcb958a1edf/584acfc48b82595af77f2030/6257bf1efec79d5baf22309f8f327ce5/favorite.png" /></button>
<p><a id="clicks"><?php echo getClickCount(); ?></a></p>

DEMO HERE

在此先感谢您的帮助,我几天前就在网上寻找它,但我没有...

亚历山大

最佳答案

counter.php

<?php

    function getClickCount() {
        return (int)file_get_contents("counter.txt");
    }

    function incrementClickCount() {
        $counter = getClickCount() + 1;
        file_put_contents("counter.txt", $counter);
    }

    if(!empty($_POST)) {
        if($_POST['click'] == 'true') {
            incrementClickCount();
            echo getClickCount();
        } else {
            echo getClickCount();
        }
    }

?>

counter.txt

0

index.php

<html>
   <head>
      <title>Click Counter</title>
   </head>
   <body>
      <button type="button" onClick="onClick()" title="Vous aimez la couverture?" class="btn"><img id="heart" src="https://trello-attachments.s3.amazonaws.com/568304b85fa72dcb958a1edf/584acfc48b82595af77f2030/6257bf1efec79d5baf22309f8f327ce5/favorite.png" /></button>
      <p><a id="clicks"></a></p>

      <script>
          function onClick() {
              loadClicks(true);
          }

          function loadClicks(isClicked) {
              var click = isClicked === true ? true : false;
              $.ajax({
                 url: 'counter.php',
                 type: 'POST',
                 data: {
                   'click': click
                 },
                 success: function(response) {
                   $('#clicks').text(response);
                 }
              });
          }

          loadClicks(false);
      </script>
   </body>
</html>

代码解释

每当您单击该按钮时,就会有一个ajax 请求在后台异步发送到counter.php。此 PHP 文件接收请求并进行相应处理。

在代码中,我们在 ajax POST 请求中将单个数据发送到 PHP 文件,这是一个 bool 数据,它是根据按钮被点击等条件设置的。

在 PHP 文件中,您将检查请求是通过单击按钮还是其他方式发生的条件。如果是按按钮,您将增加点击次数并发送点击计数器值作为响应,否则您将只发送该值。

您会注意到我在 onClick 函数中使用参数 true 调用了 loadClicks 函数,在外部调用了 false函数意味着我首先调用 loadClicks(false) 一旦脚本开始执行它只加载点击值,然后当我点击按钮 loadClicks(true) 被调用意味着递增和获取值。

当你仔细阅读它们时,你就会理解代码。

关于javascript - file.txt 中的 PHP Like 按钮计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076091/

相关文章:

javascript - 你能告诉我我的代码有什么问题吗?

php - 添加到数据库字段时使用其中之一

php - 尝试调整 PHP echo 的输出

php - 尽管密码正确,password_verify 密码始终无效

html - 标题背景和另一个要在 Wordpress 沙箱上重复的背景

javascript - 如何在 javascript 中获取日期的开始时间——考虑时区

javascript - 使用带 Sails.js v0.10 的 Skipper 上传文件 - 如何检索新文件名

javascript - 使用正则表达式验证 DD-Mon-YYYY 格式的日期

javascript - 为 Angular ng-repeat 添加一个计数器?

html - CSS 是否有更好的边框速记版本?