0

When i try to set my <span> to PHP variable, it sends it commented. The file is saved as .php and is served by apache2.4 from localhost

document.getElementById('displayName').innerHTML = " <?php echo $_SESSION['user']; ?>";

This is what i see in browser when inspecting:

<!--?php echo $_SESSION['user']; ?-->
  • Take a look at this post. Seems the same problem you have and it is solved https://stackoverflow.com/questions/12828163/why-are-my-php-tags-converted-to-html-comments . Hopes this helps – Francisco Valle Feb 07 '18 at 15:21
  • try removing the quotation marks because that implies a string, or use the single quote. – mcv Feb 07 '18 at 15:21

1 Answers1

2

You can't run PHP code with JavaScript. When the user receives the page, the server will have evaluated and run all PHP code, and taken it out.

Zaheer Abbas
  • 145
  • 3
  • 13