How do I access my PHP variable from JavaScript?
- Starred lines represent places where problem may be occurring due to my lack of knowledge of how HTML, JavaScript, and PHP interact
Here's the situation:
I'm creating a game that uses JavaScript, and I'm trying to access player information (player's name) from a database using PHP. In order to help me debug I'm not currently trying to access the database. Right now I'm just placing a variable within the PHP file to represent th开发者_运维百科e information I'll try to access in the future, a string, which is the player's name. Access to the HTML file, JavaScript file, and PHP file goes as follows:
*Files are all kept on GoDaddy hosting in the same directory, the files being named:
index.html
milktruck.js
xml_http_request.php
.
The user visits the website, which accesses the JavaScript file using the following line of code:
<script type="text/javascript" src="milktruck.js"></script>
The game starts and the user is presented with a game interface. At this point the user has not specified to enter multiplayer mode and therefore the line of JavaScript code that accesses the PHP variable and displays it has not fired.
*Nowhere in my JavaScript file or HTML file do I make reference to my PHP file, such as the reference made to the JavaScript file in the HTML file. Do I need to make reference to the PHP file? Does using xml_http_request.php
make any difference?
I've tried adding the following line of code in my JavaScript file:
var simple = '<?php echo $simple; ?>';
and the following in my PHP file:
$simple = 'simple string';
but when I run the game and click on multiplayer mode instead of the string 'simple string' being displayed the string "php echo $simple;" is displayed (with all the other characters, Stack Overflow won't let me put it in there). JavaScript is recognizing that PHP code as a string, not a line of code.
What's wrong here?
Here's a link to my game if you want to see for yourself. Just click on the "multiplayer mode" button below the screen.
http://alterearth.net
So you're adding the php code into a .js file?
If so then the server won't recognise the tag unless the <?php ?>
is in a .php file. This is unless you change server settings.
You can make the PHP code echo the Javascript code with the variable in it.
Example:
echo "<script> var x = $variable; </script>";
you cant embed php in javascript file, because javascript file will be interpreted by the browser which dont know anything about php. instead you should put javascript in php file. e.g.
echo "<script>userinfo = 'xxxxxxxxx'</script>";
then you can get the userinfo object in javascript. you can do this becuase php file will produce the html which will be interpreted by the browser.
Drupal offers a simple API to port PHP variables to JS, take a look at drupal_add_js
if you want this functionality more layered and flexible.
I see three possible answers:
1) you can set up PHP so that it pays attention to the .js file, like this guy talks about: http://www.geekhelpguide.com/php/handle-images-css-js-php-httpd-conf-using-htaccess/
or
2) make a hidden variable in your .php file like this: <input id="myvariable" type="hidden" value="<?php echo $simple;?>">
and then reference your hidden variable with the javascript, like this (if you use jquery): $("#myvariable").val();
or
3) embedding your javascript inside your .php file like some of the other folks who have given answers have suggested.
I hope that helps. Please mark as answer if it did.
精彩评论