Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计数和显示按钮点击页面_Javascript_Php_Mysql_Counter - Fatal编程技术网

Javascript 计数和显示按钮点击页面

Javascript 计数和显示按钮点击页面,javascript,php,mysql,counter,Javascript,Php,Mysql,Counter,我正在寻找一个非常简单的解决方案来跟踪和显示网页上按钮的点击次数。我搜索了谷歌,发现的所有解决方案都比我要寻找的复杂得多 基本上,我需要它做的就是跟踪每次单击一个按钮的情况,将总点击量的值存储在数据库中,然后在同一页面上显示该值。我猜我需要使用PHP/MySQL来完成这个任务,有人能给我指出正确的方向吗 谢谢大家! 这里是在服务器上保存一个号码的最简单方法。或者换句话说,这并不是那么简单。尝试一些代码,然后问一个更具体的问题 注:w3schools不是最好的html资源,但它确实提供了我找到了一

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

基本上,我需要它做的就是跟踪每次单击一个按钮的情况,将总点击量的值存储在数据库中,然后在同一页面上显示该值。我猜我需要使用PHP/MySQL来完成这个任务,有人能给我指出正确的方向吗


谢谢大家!

这里是在服务器上保存一个号码的最简单方法。或者换句话说,这并不是那么简单。尝试一些代码,然后问一个更具体的问题


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

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

PHP: HTML:


这可以通过PHP/MySQL/jQuery实现。我还没有测试过这段代码,它在跟踪快速点击时不会非常有效,但它应该可以让您开始:

在jquery调用下面包含click.js 设置一个名为click_log的SQL表,该表包含用户id INT和时间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>
单击.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;

?>

希望这能帮助您开始

到目前为止您尝试了什么代码?可以使用jQuery。设置一个计数器变量,如var i=0;然后在文档上使用click事件,并在click事件或计时器上使用ajax提交$document.on'click',function{i++;/*preform ajax调用PHP*/};。然后将你的php记录到你的dbI中我没有尝试过任何代码,因为到目前为止我发现的所有解决方案对于我试图完成的任务都过于复杂。你对你正在做的事情一无所知,对吗?如果这是真的,那也没关系,但如果你这么诚实,让我们知道你需要一些初学者的网站参考资料,这将真正帮助我们。我有种感觉,你已经陷入了困境。我建议谷歌搜索一些东西,比如如何用PHP构建网站以及如何使用jQuery,因为它比Vanilla JS更快、更容易学习。如果你想要Ajax,你就会想要jQuery,这样你就不会有更多的问题?我不这么认为。我并不是要构建一个完整的PHP站点,只是一小段用于跟踪和存储按钮计数的代码。我想这很清楚,我对我在做什么一无所知。哦,我明白了,所以你只想数一数按钮的点击次数。以前好像没有这么说。伙计。。。你这样做太过分了。如果您所做的只是计算按钮的点击次数,那么您需要使用Ajax,而不是计算文件的加载量,这正是您上面所做的。我在顶部评论中的例子仍然应该给你一个小线索,但基本上,就是不要让锚去任何地方。让它调用ajax,然后让点击的php计数保存到DB,然后决定是重新路由还是简单地返回ajax JSYK3HH的计算。感谢SpYk3HH,我将使用它。这非常有用,谢谢Andrew.$document.click或$window.click可能更准确,因为CSS有时可能会允许正文不覆盖整个站点区域。还有,很好的答案和很好的时间来写所有这些,哈哈!太棒了,@SpYk3HH!我已经编辑了我的答案。谢谢:另外,我通过添加标记澄清了第一段代码@APAD1
$(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);
        }
      });

  }
}
<?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");
  }


?>
<?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;

?>