-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?

1
  • 3
    After the <body> tag : <?php echo $_SESSION[ "my_var" ]; ?>. By the way, you should change your profile image. Commented Jun 11, 2015 at 14:17

3 Answers 3

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.

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

Comments

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

6 Comments

"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.
Sidenote: that's not my downvote.
@Fred-ii- Thanks for the hint, answer updated accordingly! no prob :)
Usually when making a mistake, you mark an edit as an edit, and possibly using the <strike>code/text</strike> 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
Agree! will keep in mind, and follow as well
|
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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.