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?
asked Mar 30, 2014 at 18:25
Matt Kent
1,1851 gold badge11 silver badges27 bronze badges
-
1Of course. Why don't you try it and see?John Conde– John Conde2014年03月30日 18:26:18 +00:00Commented Mar 30, 2014 at 18:26
-
1Compile that code, haha.Shahar– Shahar2014年03月30日 18:26:43 +00:00Commented Mar 30, 2014 at 18:26
-
1Should work fine, just remember to quote it if it's a stringadeneo– adeneo2014年03月30日 18:29:43 +00:00Commented Mar 30, 2014 at 18:29
-
That's it the quote. Forgot the quote... ;)Matt Kent– Matt Kent2014年03月30日 18:30:14 +00:00Commented Mar 30, 2014 at 18:30
1 Answer 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>
answered Mar 30, 2014 at 18:28
René Roth
2,1961 gold badge20 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default