Using PHP Session in JQuery using Json Encode

Using PHP Session in JQuery using Json Encode

I am trying to append one value from json encode. Here is my code:

session.php
  1. echo json_encode($_SESSION);
  2. /* output */
  3. "user_id":"1","firstname":"John","lastname":"Doe","email":"jdoe@email.com","role":"administrator","status":"active"}
How do I then append just the firstname to index.html?

  1. <script type='text/javascript'>
  2. $(document).ready(function () {
  3. /* call the php that has the php array which is json_encoded */
  4. $.getJSON('session.php', function (session) {
  5. /* data will hold the php array as a javascript object */
  6. .each(session, function (session_key, session_val) {
  7. $("#user").append(
  8. '<li class="dropdown"><a href="#" class="dropdown-toggle pr-0" data-toggle="dropdown"><i class="fa fa-user top-nav-icon text-success mr-10"></i>' + session.firstname + '</a></li>'
  9. );
  10. });
  11. });
  12. });
  13. </script>
This is outputting the firstname multiple times. What am I doing wrong?