1

Possible Duplicate:
PHP script not working in HTML file

I have this:

<?php
if(isset($_POST['submit'])){
   $f = fopen('counter.txt', 'r+');
   flock($f, LOCK_EX);
   $total = (int) fread($f, max(1, filesize('counter.txt')));
   if (isset($_POST['submit'])) {
      rewind($f);
      fwrite($f, ++$total);
   }
   fclose($f);
}
?>

AND it works in the txt file. It counts correctly the clicks. BUT this:

Times submited <?php echo $total; ?>.

doesn't work in my index.html file where I have my form. Please can you help?

This the line of my files:

  1. form (index.html)
  2. a .php file
  3. header("Location: thankyou.php")
  4. then back to index.html
Community
  • 1
  • 1
Paul
  • 21
  • 1

2 Answers2

1

Well first off, php isn't going to be executed in an html file. You need to make the file a php one so the server treats it way.

That's why it's working in the text file.

If it has to be html, you can use this:

http://php.about.com/od/advancedphp/p/html_php.htm

Secondly, the $total variable isn't global, so it won't be known in a different php file, you'll either need to pass it using get or post, or include the php in the first html file.

I'd go with the latter.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
will
  • 10,260
  • 6
  • 46
  • 69
  • Well that makes sense, it's not surprising they don't let you play around d too much with the htaccess file. Try moving the php into the html file and changing it to php from html. – will Oct 23 '12 at 00:50
  • Do if(! Empty($_post['submit']) {} Also, use get not post. – will Oct 23 '12 at 00:52
  • I am so confused and so newbie in .php.... – Paul Oct 23 '12 at 01:25
0

To count the number of clicks without writing to a file you will need to store the number of clicks in the session because it is not global, nor does it persist beyond a single request. This would all need to be done in the .php file.

<?php
$total = 0;

if(isset($_POST['submit'])){
  1. Read the value from session back into $total before incrementing.

    if(isset($_SESSION["total"]))
       $total = $_SESSION["total"];
    

2 Increment $total

  $total++;
  1. Store $total back into session

    $_SESSION["total"] = $total;
    }
    
  2. Then you can print out the total.

    echo 'Times submited: ' . $total .'.';
    ?>
    
CrazyWebDeveloper
  • 378
  • 1
  • 2
  • 11
  • $total = 0; if(isset($_POST['submit'])){ if(isset($_SESSION["total"])) $total = $_SESSION["total"]; $total++; $_SESSION["total"] = $total; } echo 'Times submited: ' . $total .'.'; **** It didn't work. Still doesn't appear in index/html . Thank you very much for your time though. – Paul Oct 23 '12 at 00:53