-1

I've got a simple .php file with Rock, Paper, Scissors game (some schoolwork) that tracks your score and a .html file with buttons to choose from R/P/S.

Score is tracked via session variable, is there a simple way to pass the value of this variable and display it on the html page?

Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131

3 Answers3

1

You should generate your HTML page from PHP file too. That's how php works. That way you can print out PHP variables. HTML is no programming language - it's just markup language...like ...hmmm..XML.

MilanG
  • 6,559
  • 2
  • 29
  • 55
1

You can add addhandler in .htaccess for html files to treat it as php file, see example:

AddHandler application/x-httpd-php .html .htm

but instead you can, avoid above and change the file extension from .html to .php you can pass it as query string GET, see example below:

http://example.com/myphppage.php?key=value

or you can access session by adding session_start() at starting of page

kamal pal
  • 4,049
  • 5
  • 22
  • 39
  • 1
    *"The answer is NO. You cannot get PHP Variables in HTML"* - That's not entirely true. You can instruct Apache to treat `.html|.htm` files as PHP files. – Funk Forty Niner Jun 11 '15 at 14:22
  • 1
    Sidenote: that's not my downvote. – Funk Forty Niner Jun 11 '15 at 14:22
  • @Fred-ii- Thanks for the hint, answer updated accordingly! no prob :) – kamal pal Jun 11 '15 at 14:28
  • You're welcome, you should have marked your edit about treating html files as php *as an edit*. People who visit your answer may think why your answer was downvoted. – Funk Forty Niner Jun 11 '15 at 14:29
  • 1
    Usually when making a mistake, you mark an edit as an edit, and possibly using the `code/text` tags on the originally posted code/text that you may have a made a mistake. That tells the community that, well.. you made a mistake. Everybody makes mistakes, even I do at times. If we didn't make them, we wouldn't have learned anything, *right?* ;-) *cheers* – Funk Forty Niner Jun 11 '15 at 14:34
  • 1
    Agree! will keep in mind, and follow as well – kamal pal Jun 11 '15 at 14:36
0

If you save your .html file as a .php file, you can do this quite easily by adding

<?php echo $_SESSION['score']; ?>

into your HTML. For example:

<p><?php echo $_SESSION['score']; ?></p>
LeDoc
  • 847
  • 1
  • 11
  • 21