Jump to content

  • Log In with Google      Sign In   
  • Create Account


Sharing Variables With JavaScript & PHP


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Shippou   Members   -  Reputation: 1378

Like
1Likes
Like

Posted 24 May 2013 - 08:49 AM

 I'm in the middle of developing a JavaScript game using the canvas features, have ran into issues related to I/O .

 In JavaScript I have no file handling capabilities, no way to create, modify, or access data bases, and no real objects .

 

 I was wondering what the easiest way of passing variables back and forth between PHP & JavaScript / JavaScript & PHP .

 

 I have seen a couple examples doing a Google search, but no good explanation on how it's done.

 From what I can tell the way it's done it to output PHP code in the JavaScript ""document.write('PHP code here');"" , and output JavaScript code in the PHP ""echo 'JavaScript code here' ;"" .

 

 How does this work, and is there an easier way this can be done ? ( without AJAX )

 

 Thank you for your time.


 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 27930

Like
2Likes
Like

Posted 24 May 2013 - 09:19 AM

Your PHP code is running on the webserver whenever a user requests a page. Your Javascript code is running in the client's browser.

If you use document.write('PHP code here');, then all that will happen is that PHP code will appear on the clients screen.

In order to actually execute some PHP code on the server, the javascript code has to make a request to the webserver. You can either do this by loading a whole new page (as if the user clicked a link), or by using AJAX etc to load a new page in the background and extract information from it.

 

For the other direction, yes, the PHP code is typically outputting a HTML document, so you can echo a < script> tag with JS code in it, which will be executed by the client's browser when it loads the document.


Edited by Hodgman, 24 May 2013 - 09:25 AM.


#3 Wooh   Members   -  Reputation: 575

Like
0Likes
Like

Posted 24 May 2013 - 09:20 AM

Can't you use cookies?

#4 wicked357   Members   -  Reputation: 1146

Like
0Likes
Like

Posted 24 May 2013 - 11:25 AM

Aside from doing AJAX calls, you could use HTML5 Web Storage to store data. Have you looked into that? If you are trying to keep track of things like scores for top scores amongst all players around the world then yes you would want to probably use AJAX to send data to a PHP script (runs in the background as stated above).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS