Arthur A. Bergamaschi
Arthur A. Bergamaschi

Reputation: 125

How to use a PHP var to on a javascript? (PHP code)

I'm trying to create a cookie in JavaScript, but it isn't working. All the stuff works, but I'm having problems with JavaScript.

This is the code for my page. The mysql connection works well, I've just masked the entries. http://pastebin.com/HfbZyVQZ

How do I can use the '$playername' with the 'setPlayer()'?

Upvotes: 1

Views: 57

Answers (2)

Liglo App
Liglo App

Reputation: 3819

the better one is:

var anyVariable = <?php echo json_encode($anyVariable); ?>;

It will handle correctly strings, booleans, numbers and arrays.

Upvotes: 1

Eddie
Eddie

Reputation: 26854

you can echo it

var playername = <?php echo $playername;?>

then you can use the var playername for your js.

Hope this help.

Upvotes: 0

Related Questions