点击页面上的计数和显示按钮


Count and display button click on page

我正在寻找一个非常简单的解决方案来跟踪和显示网页上按钮的点击次数。我搜索了谷歌,发现的所有解决方案都比我想要的要复杂得多。

基本上,我所需要做的就是跟踪每次点击一个按钮的时间,将总点击量的值存储在数据库中,然后在同一页面上显示该值。我想我需要使用PHP/MMySQL来实现这一点,有人能给我指明正确的方向吗?

谢谢!

这可以通过PHP/MySQL/jQuery来完成。我还没有测试过这些代码,它在跟踪快速点击的爆发方面不会很有效,但它应该会让你开始:

  • 在网页页脚中包含jQuery
  • 在jquery调用下包括click.js(如下)
  • 设置一个名为click_log的SQL表,该表包含user_id (INT)time (DATETIME)

在文档底部包含此代码。它为增量脚本提供了一个用户ID(假设您手边有一个用户的ID作为变量):

<script>
    $(function () {
      // Event handler for the click
      $(document).click(function(e){
        // Feed the increment function the user's ID
        increment(<?= $user_id ?>);
      });
    })
</script>

在您希望显示总计数的任何位置包括计数器div,并在其中包括get_count.php

<div id="counter">
    <?php include("get_count.php?user_id=".$user_id); ?>
</div>

点击.js-jQuery函数,通过AJAX 调用下面的增量脚本

$(function () {
  // Define an increment function to accept a user_id
  function increment(user_id){
      // Create a data object
      data = {
        user_id = user_id
      }
      // Deliver the payload
      $.ajax({
        type: "POST", 
        url: "increment.php",
        dataType:"JSON", 
        data: data, 
        success:function(response){
          $("#counter").empty().load("get_count.php?user_id="+user_id);
        }
      });
  }
}

increment.php-从AJAX请求注册点击数据库的php脚本

<?php
  // Set the header to accept JSON
  header("Content-type: application/json");
  // Insert your database connection here (should probably use mysqli)
  $host     =  "localhost"; // or IP or whatever
  $db_user  =  "admin";
  $db_pass  =  "password";
  $db_name  =  "your_db";
  mysql_connect($servername, $db_user, $db_pass) or mysql_error();
  mysql_select_db($db_name) or db_name();
  // Grab user id from post data
  $user_id = $_POST["user_id"];
  // Write the query
  $sql = "INSERT INTO click_log (user_id, time) VALUES ('".$user_id."', CURRENT_DATE());"
  // Encode a JSON response if the query is successful
  if(mysql_query($sql)){
    echo json_encode("Success");
  }

?>

get_count.php-要加载到计数器div中的文件,该文件显示用户的总点击量

<?php 
  // Get the user id from the URL ($_GET)
  $user_id = $_GET["user_id"];
  // Write the SQL to count the number of rows ('clicks') with that user
  $sql = "SELECT count(*) FROM click_log WHERE user_id = '".$user_id."';";
  // Run the query
  $result = mysql_query($count_sql);
  // Format the result and set it a variable
  $total_clicks = mysql_result($result, 0);
  // Print the total clicks by that user
  echo $total_clicks;
?>

希望这能帮助你开始!:)

以下是在服务器上保存数字的最简单方法。http://www.w3schools.com/php/php_ajax_poll.asp或者换句话说,事情并没有那么简单。尝试一些代码,然后问一个更具体的问题。

注意w3schools不是最好的html资源,但它确实提供了

我找到了一个完全符合我需求的文本文档解决方案。我已经实现了它,但问题是,在最初单击按钮后,操作仍保留在URL中,所以要增加计数,只需要刷新页面。预防这种情况最简单的方法是什么?

PHP:

if ( ! file_exists('./count.txt'))
{
    file_put_contents('./count.txt','0');
}
if (trim($_GET['action']) == 'increment')
{
    counter('increment');
}
function counter($action)
{
    if ($action == 'count')
    {
        return file_get_contents('./count.txt');
    }
    else
    {
        file_put_contents('./count.txt', (string) (((int) file_get_contents('./count.txt')) + 1));
    }
    return true;
}
?>

HTML:

<span id="button"><a href="index.php?action=increment#page5">Click Here</a></span>