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
6
  • 2
    We'd need more HTML, in my opinion, to help. Commented Oct 23, 2012 at 0:20
  • 2
    PHP code doesn't (usually) run in .html files. And variables such as $total do not persist across different page requests; the counter file needs to be read again. Commented Oct 23, 2012 at 0:22
  • You can store $total in a cookie or session variable to enable access in any page. Also use .php for it to work Commented Oct 23, 2012 at 0:23
  • Yes. I think this might be a scoping problem either within code or across requests (use session). Declare a a global $total if you're not doing it. Commented Oct 23, 2012 at 0:24
  • 1
    Move index.html to index.php. Then you can do what you need. Commented Oct 23, 2012 at 0:25

2 Answers 2

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.

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

3 Comments

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.
Do if(! Empty($_post['submit']) {} Also, use get not post.
I am so confused and so newbie in .php....
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 .'.';
    ?>
    

1 Comment

$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 <?php echo $total; ?> doesn't appear in index/html . Thank you very much for your time though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.