0

I'm trying to build a tiny skeleton framework for a friend, where each time a button is pressed a certain animation is played. He wants a way to count the number of times the button is clicked, as well, but I can't seem to get that part working. What am I doing wrong?

<?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>
2
  • What isn't working? Any errors given? Or does it just not increment your stored number? Commented Aug 4, 2010 at 21:20
  • Currently, the file isn't opening. Before, the issue was that nothing was happening at all upon button click. Commented Aug 4, 2010 at 21:54

3 Answers 3

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

First of all use the form with method="post", or change $_POST[] to $_GET[] in your Script.

And If your Button is not a Submit button, then you are not submitting the form. So I've changed type="button" to type="submit".

Should work

Sign up to request clarification or add additional context in comments.

5 Comments

This is helping the issue, but now I get the error "Can't open file."
you have to create an empty file named clickcount.txt.
"Cant open file" means that there is no file named clickcount.txt so it cant open it to read and write it. And if you create the file manually it can rewrite it ;)
But there IS a file there. :(
in the same directory where this script is?
1

The code looks fine, I tested it and it worked for me. I suggest:

  • Make sure the file isn't read-only.
  • Make sure the file is called "clickcount.txt"
  • Make sure it's in the same folder as your script.

Comments

0

It would be helpful to know the error but a shot in the dark - it could be a problem with Write permissions?

also, change to:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.