Using PHP Session in JQuery using Json Encode
I am trying to append one value from json encode. Here is my code:
session.php
- echo json_encode($_SESSION);
- /* output */
- "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?
-
<script type='text/javascript'>
-
$(document).ready(function () {
-
/* call the php that has the php array which is json_encoded */
-
$.getJSON('session.php', function (session) {
-
/* data will hold the php array as a javascript object */
-
.each(session, function (session_key, session_val) {
-
$("#user").append(
-
'<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>'
-
);
-
});
-
});
-
});
-
</script>
This is outputting the firstname multiple times. What am I doing wrong?