javascript 在外部 JS 文件中传递 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6000871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a PHP Variable in external JS file
提问by Petra
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.comthis 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.comits working fine. For some reason the $company['website']
value isn't being passed :(
我在 track.php(或 track.js)中的框架不会加载 中指定的 url $company['website']
,但如果我将其更改为http://www.google.com,它就可以正常工作。由于某种原因,该$company['website']
值没有被传递:(
回答by Ibu
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
跟踪文件
<?php
// javascript generator
Header("content-type: application/x-javascript");
?>
document.write('<IFRAME SRC="<?php echo $company['website'] ?>" WIDTH="300" HEIGHT="400"></IFRAME>');
回答by AndersTornkvist
PHP file (don't forget echo and quoting):
PHP 文件(不要忘记回显和引用):
<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>');
回答by Naim Zard
You should fix this line:
var pass_this_variable = <?php echo $company['website']; ?>;
你应该修复这一行:
var pass_this_variable = <?php echo $company['website']; ?>;
Adding echo
and it should work
添加echo
它应该可以工作
回答by Vasanthan.R.P
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 中使用它。确保对输入进行消毒。
回答by Omer Aslam
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 为您提供 ajax 功能,用于读取 PHP 或文本文件。为什么不在iframe
PHP 文件中创建 HTML并解析变量,然后收回响应并将其“抛出”到 div 中。
回答by MIlan Modh
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;