JavaScript中的按钮单击计数器

JavaScript中的按钮单击计数器,javascript,html,Javascript,Html,在这里,我发现了一个JavaScript按钮点击计数器,它计算按钮点击次数,并将点击次数保存在称为web存储的东西中,我不知道这到底是什么 有一件事我可以肯定,这个脚本只适用于一台计算机,这意味着如果我点击按钮10次,那么如果任何其他访问者点击按钮,它将不会向他显示我以前点击过的点击次数 现在我需要的是,不管是使用javascript还是php,点击次数都应该保存在我服务器的文本文件中,以后任何其他访问者访问HTML页面时,他也应该得到文本文件中的相同数字 这里是带有代码的HTML页面 <

在这里,我发现了一个JavaScript按钮点击计数器,它计算按钮点击次数,并将点击次数保存在称为web存储的东西中,我不知道这到底是什么

有一件事我可以肯定,这个脚本只适用于一台计算机,这意味着如果我点击按钮10次,那么如果任何其他访问者点击按钮,它将不会向他显示我以前点击过的点击次数

现在我需要的是,不管是使用javascript还是php,点击次数都应该保存在我服务器的文本文件中,以后任何其他访问者访问HTML页面时,他也应该得到文本文件中的相同数字

这里是带有代码的
HTML
页面

<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter()
{
if(typeof(Storage)!=="undefined")
  {
  if (localStorage.clickcount)
    {
    localStorage.clickcount=Number(localStorage.clickcount)+1;
    }
  else
    {
    localStorage.clickcount=1;
    }
  document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s).";
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";
  }
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
</body>
</html>

函数clickCounter()
{
if(类型(存储)!=“未定义”)
{
if(localStorage.clickcount)
{
localStorage.clickcount=Number(localStorage.clickcount)+1;
}
其他的
{
localStorage.clickcount=1;
}
document.getElementById(“结果”).innerHTML=“您已单击按钮”+localStorage.clickcount+“时间”。”;
}
其他的
{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持web存储…”;
}
}
点击我

单击按钮查看计数器的增加

关闭浏览器选项卡(或窗口),然后重试,计数器将继续计数(不会重置)

简单地说,

HTML页面上有一个按钮

如果访问者A点击了5次并关闭页面


然后,访问者B访问该页面,他应该首先获得数字5,然后当他单击时,该数字应该自动计数并保存。

在用户关闭该页面之前,您应该将“单击”计数存储到数据库中,否则计数将清除为零。如果下次其他用户打开页面时,您将计数存储在数据库中,则可以从上一次计数开始单击计数。我希望您明白我的意思。谢谢sujathan。

您对本地存储有误解。本地存储是将信息存储到客户端浏览器的一种方式,该浏览器对于站点的每个访问者都是唯一的。它与用户之间共享的服务器数据库完全不同。在您的情况下,用户B无法知道用户A单击了多少次,除非您将用户A的信息存储到服务器数据库中,并在用户B单击页面时查询这些信息。

这是一件简单的事情。这个答案来源于学校。这里使用的是AJAX和PHP。为了保存该值,我们使用一个名为“vote_result.txt”的文本文件

index.html

<html>
    <head>
    <script type="text/javascript">
    function getVote(int)
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("poll").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","poll_vote.php?vote="+int,true);
    xmlhttp.send();


    if(typeof(Storage)!=="undefined")
      {
      if (localStorage.clickcount)
        {
        localStorage.clickcount=Number(localStorage.clickcount)+1;
        }
      else
        {
        localStorage.clickcount=1;
        }
      document.getElementById("result").innerHTML="You have voted " + localStorage.clickcount + " times before this session";
      }
    else
      {
      document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";
      }
    }

    </script>

    </head>
    <body bgcolor=#5D003D>
    <div id="poll">

    <p>Click the button to see the counter increase.</p>
    <p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p><form>
    <input type="Button" class="voteButton" name="vote" value="Vote" onclick="getVote(this.value)" />
    </form>
    </div>
    </body>
    </html>

函数getVote(int)
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“poll”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“poll_vote.php?vote=“+int,true”);
xmlhttp.send();
if(类型(存储)!=“未定义”)
{
if(localStorage.clickcount)
{
localStorage.clickcount=Number(localStorage.clickcount)+1;
}
其他的
{
localStorage.clickcount=1;
}
document.getElementById(“结果”).innerHTML=“您在此会话之前已投票”+localStorage.clickcount+“次”;
}
其他的
{
document.getElementById(“结果”).innerHTML=“对不起,您的浏览器不支持web存储…”;
}
}
单击按钮查看计数器的增加

关闭浏览器选项卡(或窗口),然后重试,计数器将继续计数(不会重置)

poll\u vote.php

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>    
<table>
<tr>
<td><div id="votesMsg">Total Votes  :</div></td>
<td><div id="votesCounter">
<?php echo($yes); ?></div>
</td>
</tr>
</table>
<input type="Button" class="voteButton" name="vote" value="Vote again !!!" onclick="getVote(this.value)" />

总票数:
然后在工作目录中,创建一个名为poll_result.txt的文件


就这些。现在在localhost中运行此页面。

您将需要服务器端代码来产生此效果。你用什么语言?PHP?ASP?@Samon Souza您需要创建一个
ajax调用
,将数据
发送到服务器
。然后服务器需要评估响应,打开文件,写入文件,然后关闭文件。现在,每次请求
页面时
都需要将文本包含在该文件中,并将文本用作计数器。是的,请告诉我如何操作,因为我是初学者。如果你也这么做的话,我会非常满意的:这是一个sanboxed环境,每个域(默认情况下)大约有2-10MB的数据可以保存到(JSON格式)。本地存储是ofc。只有一个用户可以访问。但也可以在浏览器存储设置中清除。是的,您的回答是正确的,但我如何才能做到这一点。你能帮我做这个吗。请显示meDo的代码。您是否使用某种数据库?Mysql?因为您使用的是php,这里有一个很好的教程祝您好运!在这里的堆栈溢出,人们不只是交给你的代码。相反,他们指出你的困境并帮助你前进。