0

I am making a game for my computing class. The user has to change settings of their robot before playing.

As the settings file where the user customises their robot is a separate PHP file I was wondering if there was a way to assign the $_SESSION variables to JavaScript variables like so:

var tractionType = <?php echo $_SESSION['traction']; ?>; ...

Is this possible?

4
  • 1
    Of course. Why don't you try it and see? Commented Mar 30, 2014 at 18:26
  • 1
    Compile that code, haha. Commented Mar 30, 2014 at 18:26
  • 1
    Should work fine, just remember to quote it if it's a string Commented Mar 30, 2014 at 18:29
  • That's it the quote. Forgot the quote... ;) Commented Mar 30, 2014 at 18:30

1 Answer 1

1

Sure, you can either create the whole JS file from PHP, as in

<script type="text/javascript" src="js/settings.php"></script>

or you put those variables inline

<script type="text/javascript">
    var my_variable=<?=(int)$_SESSION['my_variable']?>;
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.