This question already has an answer here:
这个问题已经有了答案:
- How to pass variables and data from PHP to JavaScript? 16 answers
- 如何将变量和数据从PHP传递到JavaScript?16个问题
I've read lots of thread on here but I am still unable to get a variable passed from PHP to an external JS file and wondered if someone could assist?
我已经在这里读了很多线程,但是我仍然无法将一个变量从PHP传递到一个外部的JS文件,我想知道是否有人能提供帮助?
In my PHP file I have the following;
在PHP文件中,我有以下内容;
<script type="text/javascript">
var pass_this_variable = <?php $company['website']; ?>;
</script>
<script type="text/javascript" src="/js/track.js"></script>
In the JS file I have the following;
在JS文件中,我有以下内容;
document.write('<IFRAME SRC="$company['website']" WIDTH="300" HEIGHT="400"></IFRAME>');
What I am trying to achieve is an IFRAME be opened and populated with what is contained within $company['website']. I know I can just use IFRAME directly in the PHP file, but this isn't what I have been tasked with for my homework. When I do use IFRAME directly in the PHP file it works fine, and if I specify a static URL in the JS file such as http://www.google.com this also works fine.
我想要实现的是一个IFRAME被打开并填充$company['website']中包含的内容。我知道我可以直接在PHP文件中使用IFRAME,但这并不是我的家庭作业的任务。当我在PHP文件中直接使用IFRAME时,它可以正常工作,如果我在JS文件(如http://www.google.com)中指定一个静态URL,也可以正常工作。
Can anyone assist? Thanks
谁能帮助?谢谢
EDIT:
编辑:
Thanks for the answers so far, however I'm still unable to get it working :(
到目前为止,谢谢你的回答,但是我还是无法让它发挥作用。
The frame that I have in track.php (or track.js) won't load the url thats specified in $company['website']
, yet if I change it to http://www.google.com its working fine. For some reason the $company['website']
value isn't being passed :(
我跟踪的框架。php(或track.js)不会加载$company['website']中指定的url,但如果我将其更改为http://www.google.com,它会正常工作。由于某种原因,$company['website']的价值并没有被传递:
6 个解决方案
#1
9
if you want your external javascript to be dynamic you can make it a php file and give the correct header, example:
如果你希望你的外部javascript是动态的,你可以把它做成一个php文件并给出正确的头,例如:
<script type="text/javascript" src="/js/track.php"></script>
track.php
track.php
<?php
// javascript generator
Header("content-type: application/x-javascript");
?>
document.write('<IFRAME SRC="<?php echo $company['website'] ?>" WIDTH="300" HEIGHT="400"></IFRAME>');
#2
6
PHP file (don't forget echo and quoting):
PHP文件(不要忘记echo和引号):
<script type="text/javascript">
var pass_this_variable = '<?php echo $company['website']; ?>';
</script>
<script type="text/javascript" src="/js/track.js"></script>
JS file (use pass_this_variable instead):
JS文件(使用pass_this_variable):
document.write('<IFRAME SRC="'+pass_this_variable+'" WIDTH="300" HEIGHT="400"></IFRAME>');
#3
2
You should fix this line: var pass_this_variable = <?php echo $company['website']; ?>;
应该修改这一行:var pass_this_variable = ;
Adding echo
and it should work
添加echo就可以工作了
#4
2
JavaScript provides you the functionality of ajax for the purpose of reading the PHP or text files. Why don't you create the HTML iframe
inside a PHP file with your variables parsed and then take back the response and "throw" it inside a div.
JavaScript为您提供了用于读取PHP或文本文件的ajax功能。为什么不在PHP文件中使用解析的变量创建HTML iframe,然后取回响应并在div中“抛出”它呢?
#5
2
The code for your PHP file:
PHP文件的代码:
$cmp = $company['website'];
echo '<input type="hidden" id="cmp1" name="cmp1" value="' . $cmp . '" />';
The code for your JavaScript (.js) file to get the PHP file value:
获取PHP文件值的JavaScript (.js)文件代码:
var company = document.getElementById('cmp').value;
#6
2
Call a PHP file inside the JavaScript source. You can find the tutorial here:
在JavaScript源代码中调用一个PHP文件。你可以在这里找到教程:
http://www.javascriptkit.com/javatutors/externalphp.shtml.
http://www.javascriptkit.com/javatutors/externalphp.shtml。
So your code will be like this:
所以你的代码是这样的:
<script type="text/javascript" src="track.php?company=<?php echo $company['website']; ?>"></script>
In the PHP file you can fetch the value through $_GET
variable and use it in the iframe. Make sure to sanitize the input.
在PHP文件中,可以通过$_GET变量获取值,并在iframe中使用它。确保对输入进行消毒。
#1
9
if you want your external javascript to be dynamic you can make it a php file and give the correct header, example:
如果你希望你的外部javascript是动态的,你可以把它做成一个php文件并给出正确的头,例如:
<script type="text/javascript" src="/js/track.php"></script>
track.php
track.php
<?php
// javascript generator
Header("content-type: application/x-javascript");
?>
document.write('<IFRAME SRC="<?php echo $company['website'] ?>" WIDTH="300" HEIGHT="400"></IFRAME>');
#2
6
PHP file (don't forget echo and quoting):
PHP文件(不要忘记echo和引号):
<script type="text/javascript">
var pass_this_variable = '<?php echo $company['website']; ?>';
</script>
<script type="text/javascript" src="/js/track.js"></script>
JS file (use pass_this_variable instead):
JS文件(使用pass_this_variable):
document.write('<IFRAME SRC="'+pass_this_variable+'" WIDTH="300" HEIGHT="400"></IFRAME>');
#3
2
You should fix this line: var pass_this_variable = <?php echo $company['website']; ?>;
应该修改这一行:var pass_this_variable = ;
Adding echo
and it should work
添加echo就可以工作了
#4
2
JavaScript provides you the functionality of ajax for the purpose of reading the PHP or text files. Why don't you create the HTML iframe
inside a PHP file with your variables parsed and then take back the response and "throw" it inside a div.
JavaScript为您提供了用于读取PHP或文本文件的ajax功能。为什么不在PHP文件中使用解析的变量创建HTML iframe,然后取回响应并在div中“抛出”它呢?
#5
2
The code for your PHP file:
PHP文件的代码:
$cmp = $company['website'];
echo '<input type="hidden" id="cmp1" name="cmp1" value="' . $cmp . '" />';
The code for your JavaScript (.js) file to get the PHP file value:
获取PHP文件值的JavaScript (.js)文件代码:
var company = document.getElementById('cmp').value;
#6
2
Call a PHP file inside the JavaScript source. You can find the tutorial here:
在JavaScript源代码中调用一个PHP文件。你可以在这里找到教程:
http://www.javascriptkit.com/javatutors/externalphp.shtml.
http://www.javascriptkit.com/javatutors/externalphp.shtml。
So your code will be like this:
所以你的代码是这样的:
<script type="text/javascript" src="track.php?company=<?php echo $company['website']; ?>"></script>
In the PHP file you can fetch the value through $_GET
variable and use it in the iframe. Make sure to sanitize the input.
在PHP文件中,可以通过$_GET变量获取值,并在iframe中使用它。确保对输入进行消毒。