How can I use a PHP session variable in a dynamically loaded navigator?

I have a series of pages that load a navbar from an external HTML file like this:

<head>
<script>
  $(function() {
  $("#navbar").load("navbar.html");
  });
</script>
</head>
<body>
<div id="navbar"></div>
<!--other stuff here-->

      

It's all on the PHP page. However, there is a part of this navigator that I want to set the PHP variable $ _SESSION (username). Is there a way to do this?

+3


source to share


2 answers


Consider changing navbar.html

to navbar.php

, then replace the username part with:

<?php echo($_SESSION["username"] ?>

      



then

$("#navbar").load("navbar.php");

      

+3


source


You won't be able to do this in the HTML you include, of course, but you can repeat it in a PHP page on a known element:

<span class='echoedUsername'><?php echo($_SESSION["username"] ?></span>



and then apply it to an element in your nav:

$("#navbar .username").text($('.echoedUsername').text())

+1


source







All Articles