3

I have two session variables that I retrieve in a Javascript code. This is how the code is set up:

<html>
    <head>
    </head>
    <body>
        <p><?php echo $_SESSION['userid'] ?></p> --> This works and value is shown
        <p><?php echo $_SESSION['accesstoken'] ?></p> --> Value is also shown
        <script type="text/javascript">
            var userid = <?php echo $_SESSION['userid'] ?>;
            var token = <?php echo $_SESSION['accesstoken'] ?>;
            alert(userid); --> this works and shows pop up with value
            alert(token); --> this doesnt work and is undefined
        </script>
    </body>
</html>

This is the value of userid: 551234131

This is the value of my token: AAADAq39fEZA0BAAVJyvfZAiu1kIcaHG4SFVzuBWl3hXfC9W0g26JaqXwZAHuNdIhh2eFDkwyopunCsZCCW3jZADT8DQBjZCAdRTC5PkgtN4wZDZD

Before the token value is stored in the session variable it is actually held inside another javascript variable without any problem (i.e I can call that variable with alert() and the token is shown).

So transfering this value FROM javascript TO session variable = no problem. But transfering the same value FROM session variable TO Javascript = doesnt work.

At first I thought there was a problem with datatypes so I tried casting it to a string value but it doesnt work. Any idea on what could cause this situation?

1 Answer 1

8

Token is a string. So you need to put quotes around it:

var token = '<?php echo $_SESSION['accesstoken']; ?>';
Sign up to request clarification or add additional context in comments.

1 Comment

Ahah!, I was trying to cast it with String(). Thanks that fixed the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.