tags:

views:

107

answers:

3

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  A: 
<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

ahmet2106
This is helping the issue, but now I get the error "Can't open file."
Andrew
you have to create an empty file named clickcount.txt.
ahmet2106
"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 ;)
ahmet2106
But there IS a file there. :(
Andrew
in the same directory where this script is?
ahmet2106
A: 

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" />
Eric C C
+1  A: 

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.
Eton B.