0

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>
Community
  • 1
  • 1
Chris Wilson
  • 203
  • 1
  • 6
  • 14
  • When and how do you call `rollpublic`? Before or after the `span`? – putvande Feb 14 '14 at 20:50
  • any error message in console when `rollpublic` is called? – Suman Bogati Feb 14 '14 at 20:50
  • 4
    _“I want to pass a SESSION variable into JS, and then ultimately onto another page”_ – what’s the sense in that? The “other page” can access it directly from the session, can it not? – CBroe Feb 14 '14 at 20:51
  • 1
    When are you setting the session variable? It has to be set when the page is created -- if you're setting it with AJAX, that won't have an effect on the page that was already sent to the client. – Barmar Feb 14 '14 at 20:51
  • Have you tried `var player = "";` – Funk Forty Niner Feb 14 '14 at 20:55
  • there are all kinds of answers on SO and other offsite resources solving this problem. – ddavison Feb 14 '14 at 21:04

4 Answers4

3

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>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    Yes! Thank you for this very comprehensive answer! - Hopefully when future noobs search for this common issue, they'll see this. I did start the session in a file "include"d at at the top, so that wasn't it. I originally decided on the code I used because of the post here at stackoverflow that said you couldn't directly access $_SESSION from JS (which they were correct in), and you must first echo the variable on the screen, make it hidden, then grab it with innerHTML. Anyway, it's good now! Thanks. – Chris Wilson Feb 15 '14 at 02:53
1

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..

Haroon
  • 480
  • 4
  • 14
1

You may call session by ajax:

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

   return session;
}
Lev K.
  • 344
  • 4
  • 4
0

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

Community
  • 1
  • 1
jeznag
  • 4,183
  • 7
  • 35
  • 53