1

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

1
  • 2
    AJAX requests from the client and json_encode() in PHP are your friends. Beyond that, you will have to be more specific as to what you are trying to do. Commented May 18, 2013 at 20:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much George, such a simple concept it just slipped my mind! One thing though, is this an efficient way of doing things? Let's say I were to build an MMORPG using HTML5 technologies averaging 50,000 players per day, would this be a good way to do things?
@jskidd3 It really depends on the type of data you want to transfer. I personally would use ajax, but if that is not an option this would also work.
@jskidd3 No problem, and like Soriyyx said I definitely recommend jQuery for handling ajax. Ajax as a long term solution is much easier to maintain. You want to look at building an API in PHP so that you can easily grab the correct data. So for user information you would make a request to the url "yoursite.com/api/userinfo" and it would return all the user information your javascript needs. This is how sites like Twitter allow people to get tweets .etc
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.