Matt Kent
Matt Kent

Reputation: 1185

Possible to assign PHP $_SESSION variable to javascript variable...?

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?

Upvotes: 0

Views: 75

Answers (1)

Ren&#233; Roth
Ren&#233; Roth

Reputation: 2096

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>

Upvotes: 1

Related Questions