- Starred lines represent places where problem may be occurring due to my lack of knowledge of how HTML, JavaScript, and PHP interact
加星标的行表示由于我不了解HTML,JavaScript和PHP如何交互而可能出现问题的地方
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 the 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:
我正在创建一个使用JavaScript的游戏,我正在尝试使用PHP从数据库访问玩家信息(玩家的名字)。为了帮助我调试我目前没有尝试访问数据库。现在我只是在PHP文件中放置一个变量来表示我将来尝试访问的信息,一个字符串,这是玩家的名字。访问HTML文件,JavaScript文件和PHP文件如下:
*Files are all kept on GoDaddy hosting in the same directory, the files being named:
*文件全部保存在同一目录中的GoDaddy托管中,文件名称为:
index.html
milktruck.js
xml_http_request.php
.
The user visits the website, which accesses the JavaScript file using the following line of code:
用户访问网站,该网站使用以下代码行访问JavaScript文件:
<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.
游戏开始并向用户呈现游戏界面。此时,用户尚未指定进入多人模式,因此访问PHP变量并显示它的JavaScript代码行未触发。
*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?
*我的JavaScript文件或HTML文件中没有任何地方可以引用我的PHP文件,例如HTML文件中对JavaScript文件的引用。我是否需要参考PHP文件?使用xml_http_request.php会有什么不同吗?
I've tried adding the following line of code in my JavaScript file:
我尝试在JavaScript文件中添加以下代码行:
var simple = '<?php echo $simple; ?>';
and the following in my PHP file:
以及我的PHP文件中的以下内容:
$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.
但是当我运行游戏并点击多人游戏模式而不是字符串'simple string'时,显示字符串“php echo $ simple;”显示(包含所有其他字符,Stack Overflow不会让我把它放在那里)。 JavaScript认为PHP代码是一个字符串,而不是一行代码。
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.
如果您想亲眼看看,这里是我游戏的链接。只需点击屏幕下方的“多人游戏模式”按钮即可。
5 个解决方案
#1
1
You can make the PHP code echo the Javascript code with the variable in it.
您可以使PHP代码使用其中的变量回显Javascript代码。
Example:
echo "<script> var x = $variable; </script>";
echo“
#2
2
So you're adding the php code into a .js file?
所以你要将php代码添加到.js文件中?
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.
如果是这样,那么服务器将无法识别标签,除非 位于.php文件中。这是除非您更改服务器设置。
#3
0
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.
你不能在javascript文件中嵌入php,因为javascript文件将由浏览器解释,它不知道任何关于PHP的东西。相反,你应该把JavaScript放在PHP文件中。例如
echo "<script>userinfo = 'xxxxxxxxx'</script>";
echo“
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.
然后你可以在javascript中获取userinfo对象。你可以这样做因为php文件会生成将被浏览器解释的html。
#4
0
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.
Drupal提供了一个简单的API来将PHP变量移植到JS,如果您希望此功能更加分层和灵活,请查看drupal_add_js。
#5
0
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/
1)你可以设置PHP,以便它注意.js文件,就像这个人谈论:http://www.geekhelpguide.com/php/handle-images-css-js-php-httpd-conf-使用-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();
2)在.php文件中创建一个隐藏变量,如下所示:”>然后使用javascript引用隐藏变量,像这样(如果你使用jquery):$(“#myvariable”)。val();
or
3) embedding your javascript inside your .php file like some of the other folks who have given answers have suggested.
3)将你的javascript嵌入你的.php文件中,就像其他一些已经给出答案的人一样。
I hope that helps. Please mark as answer if it did.
我希望有所帮助。如果有,请标记为答案。
#1
1
You can make the PHP code echo the Javascript code with the variable in it.
您可以使PHP代码使用其中的变量回显Javascript代码。
Example:
echo "<script> var x = $variable; </script>";
echo“
#2
2
So you're adding the php code into a .js file?
所以你要将php代码添加到.js文件中?
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.
如果是这样,那么服务器将无法识别标签,除非 位于.php文件中。这是除非您更改服务器设置。
#3
0
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.
你不能在javascript文件中嵌入php,因为javascript文件将由浏览器解释,它不知道任何关于PHP的东西。相反,你应该把JavaScript放在PHP文件中。例如
echo "<script>userinfo = 'xxxxxxxxx'</script>";
echo“
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.
然后你可以在javascript中获取userinfo对象。你可以这样做因为php文件会生成将被浏览器解释的html。
#4
0
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.
Drupal提供了一个简单的API来将PHP变量移植到JS,如果您希望此功能更加分层和灵活,请查看drupal_add_js。
#5
0
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/
1)你可以设置PHP,以便它注意.js文件,就像这个人谈论:http://www.geekhelpguide.com/php/handle-images-css-js-php-httpd-conf-使用-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();
2)在.php文件中创建一个隐藏变量,如下所示:”>然后使用javascript引用隐藏变量,像这样(如果你使用jquery):$(“#myvariable”)。val();
or
3) embedding your javascript inside your .php file like some of the other folks who have given answers have suggested.
3)将你的javascript嵌入你的.php文件中,就像其他一些已经给出答案的人一样。
I hope that helps. Please mark as answer if it did.
我希望有所帮助。如果有,请标记为答案。