Question

Okay, here's something that may seem rather trivial, but it's been giving me a headache, and I must have some sort of mental block now.

I want to pass a SESSION variable into JS, and then ultimately onto another page... It's only the first part that I'm worried about for now, because I can't even get a good "alert". The variable is blank... "the page at xxxxx says ____", even though the username shows up in the HTML span tag.

So, here's the applicable code on my page. EDIT - For those asking why I would do this, I read it on (this) post...

EDIT: Solved. The method to do this is best done with:

var player = "<?php echo $_SESSION['username']; ?>";

Didn't need jQuery or other code. It is simple and direct, and shame on me for just sticking with one proposed solution from the other post. In answer to some other questions: I did already have start session() at the top of the file. The script was after the HTML/PHP code. *** ... HTML stuff...

You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>

...more HTML...

<script>
    function rollpublic()
    {
... some declarations...
        var player = document.getElementById("username").innerHTML;
        alert (player);
more JS }
</script>
Was it helpful?

Solution

Edit

Sidenote: One reason which may be the cause of your session name not appearing, may be because session_start(); is not present in your pages, nor is there any mention of it in your posted code.

Without seeing FULL code, is hard to pinpoint it.

However, here is a successful test that I performed using the following codes: (page 1 & page 2)

Page 1:

<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>

<a href="sessions_check.php">Check session name</a>

Page 2: (sessions_check.php) which will echo and alert the session name.

<?php
session_start();
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
    $(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";

alert (player);

    });
</script>

You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>

Appearing in HTML source: var player = "USERNAME";

and You are <span id='username'>USERNAME</span></div>

  • N.B.: session_start(); needs to be inside all of the pages using sessions in order for this to work properly.

(Original answer) This worked for me:

<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";

alert (player);
    });
</script>

You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>

OTHER TIPS

You may call session by ajax:

function getSession(){
   var session;
   $.get('someUrlThatReturnSession', function(sessionData) { 
      session = sessionData;
   });

   return session;
}

You can assign SESSION variable to javascrip variable..

<script>
function myFunction(){
    var a = <?php echo $_SESSION['var'] ?>;
    alert(a);
}
</script>

If you need more help let me know..

If you're using Angular JS, you can use this approach. (Provide the PHP var to the ng-init param of a hidden input)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top