在外部 JS 文件中传递 PHP 变量
我已经阅读了这里的很多帖子,但我仍然无法将变量从 PHP 传递到外部 JS 文件,并且想知道是否有人可以提供帮助?
在我的 PHP 文件中,我有以下内容;
<script type="text/javascript">
var pass_this_variable = <?php $company['website']; ?>;
</script>
<script type="text/javascript" src="/js/track.js"></script>
在 JS 文件中我有以下内容;
document.write('<IFRAME SRC="$company['website']" WIDTH="300" HEIGHT="400"></IFRAME>');
我想要实现的是打开一个 IFRAME 并填充 $company['website'] 中包含的内容。我知道我可以直接在 PHP 文件中使用 IFRAME,但这不是我的作业任务。当我直接在 PHP 文件中使用 IFRAME 时,它工作正常,如果我在 JS 文件中指定静态 URL,例如 http: //www.google.com 这也可以正常工作。
有人可以帮忙吗?谢谢
编辑:
感谢到目前为止的答案,但是我仍然无法让它工作:(
我在 track.php (或 track.js) 中的框架不会加载 $ 中指定的 url company['website']
,但如果我将其更改为 http://www.google.com 由于某种原因,$company['website']
值没有被传递:(
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?
In my PHP file I have the following;
<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;
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.
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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
JavaScript 为您提供了 ajax 功能,用于读取 PHP 或文本文件。为什么不在 PHP 文件中创建 HTML
iframe
并解析变量,然后收回响应并将其“扔”到 div 中。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.PHP 文件的代码:
用于获取 PHP 文件值的 JavaScript (.js) 文件的代码:
The code for your PHP file:
The code for your JavaScript (.js) file to get the PHP file value:
在 JavaScript 源代码中调用 PHP 文件。您可以在这里找到教程:
http://www.javascriptkit.com/javatutors/externalphp.shtml< /a>.
因此,您的代码将如下所示:
在 PHP 文件中,您可以通过
$_GET
变量获取值并在 iframe 中使用它。确保清理输入。Call a PHP file inside the JavaScript source. You can find the tutorial here:
http://www.javascriptkit.com/javatutors/externalphp.shtml.
So your code will be like this:
In the PHP file you can fetch the value through
$_GET
variable and use it in the iframe. Make sure to sanitize the input.如果您希望外部 javascript 是动态的,您可以将其设为 php 文件并提供正确的标头,例如:
track.php
if you want your external javascript to be dynamic you can make it a php file and give the correct header, example:
track.php
PHP 文件(不要忘记 echo 和引用):
JS 文件(使用 pass_this_variable 代替):
PHP file (don't forget echo and quoting):
JS file (use pass_this_variable instead):
你应该修复这一行:
var pass_this_variable = ;
添加
echo
它应该可以工作You should fix this line:
var pass_this_variable = <?php echo $company['website']; ?>;
Adding
echo
and it should work