按钮单击PHP中的计数器

时间:2010-08-04 21:15:42

标签: php html

我正在尝试为朋友构建一个微小的骨架框架,每次按下按钮时都会播放某个动画。他想要一种方法来计算点击按钮的次数,但我似乎无法让这部分工作。我做错了什么?

<?php

  if( isset($_POST['mushu']) )
  {
    echo "Working.";
    playAnimation();
    clickInc();
  }

  function playAnimation()
  {
     /* ... */;
  }

  function clickInc()
  {
    $count = ("clickcount.txt");

    $clicks = file($count);
    $clicks[0]++;

    $fp = fopen($count, "w") or die("Can't open file");
    fputs($fp, "$clicks[0]");
    fclose($fp);

    echo $clicks[0];

  }
?>

<html>

  <head>

    <title>Adobe Kitten</title>

  </head>

  <body>

    <form action="<?php $_SERVER['PHP_SELF']; ?>">
    <input type="button"
           value="Let's see what Mushu is up to."
           name="mushu">
    </form>

  </body>
</html>

3 个答案:

答案 0 :(得分:2)

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit"
           value="Let's see what Mushu is up to."
           name="mushu">
</form>

首先使用带有method =“post”的表单,或者在脚本中将$ _POST []更改为$ _GET []。

如果您的按钮不是“提交”按钮,则表示您未提交表单。所以我把type =“button”更改为type =“submit”。

应该工作

答案 1 :(得分:1)

代码看起来很好,我测试了它,它对我有用。 我建议:

  • 确保文件不是只读文件。
  • 确保该文件名为“clickcount.txt”
  • 确保它与您的脚本位于同一文件夹中。

答案 2 :(得分:0)

知道错误但在黑暗中拍摄会很有帮助 - 这可能是写权限的问题吗?

另外,改为:

<input type="submit" value="Let's see what Mushu is up to." name="mushu" />
相关问题