Getting information from PHP into HTML5 JavaScript Game

I've been mucking around with JS/Node/Socket.IO and made a little multiplayer game where you are all dots and can move around. I now want to take it a level further and add a simple login system so that I can display information from the user's login above their head to help identify different players.

Now I know that PHP is a server-side language and JavaScript is mainly client-side but I need to come up with a way to do this. What I'm really asking is, does anybody know how to get information straight from PHP into JavaScript?

Later on I might want to add levelling and store player's levels in a database and retrieve them when they connect (as opposed to using Local Storage).

Thanks very much!

Joel

You can use PHP to write JavaScript.

e.g.

   $jsVars = array(
      "one" => "hello"
   ,  "two" => "yo"
   );

   echo '<script type="text/javascript">';
   echo "var phpVars = ".json_encode($jsVars);
   echo "</script>";

Or you can utilise ajax to make requests to PHP and pass the data via JSON.

I normaly do this like this:

<script type="text/javascript">
    var name = '<?php echo $name; ?>';
</script>

Inside of a PHP file of course. I think this is easier for server than creating whole javascript with php.

If you want to pull the data in without a page load. You can use AJAX to call a php script that echoes out the data you need. You could store the username in a session variable upon successful login in php and then just echo that out when you need to.

A basic vanilla JS example would be;

function getUsername(){

   var xmlhttp;

   if(window.XMLHttpRequest){
   xmlhttp=new XMLHttpRequest();
   }

   else{
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange=function(){

      if(xmlhttp.readyState==4 && xmlhttp.status==200){
      var username=xmlhttp.responseText;
      // code to add username above player here
      }

   }

   xmlhttp.open("GET","get_username.php",true);
   xmlhttp.send();

}

Edit: Never mind. :D