在外部 JS 文件中传递 PHP 变量

发布于 2024-11-06 20:16:43 字数 983 浏览 1 评论 0原文

我已经阅读了这里的很多帖子,但我仍然无法将变量从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

听不够的曲调 2024-11-13 20:16:44

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.

少跟Wǒ拽 2024-11-13 20:16:44

PHP 文件的代码:

$cmp = $company['website'];
echo '<input type="hidden" id="cmp1" name="cmp1" value="' . $cmp . '" />';

用于获取 PHP 文件值的 JavaScript (.js) 文件的代码:

var company = document.getElementById('cmp').value;

The code for your PHP file:

$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:

var company = document.getElementById('cmp').value;
心不设防 2024-11-13 20:16:44

在 JavaScript 源代码中调用 PHP 文件。您可以在这里找到教程:

http://www.javascriptkit.com/javatutors/externalphp.shtml< /a>.

因此,您的代码将如下所示:

<script type="text/javascript" src="track.php?company=<?php echo $company['website']; ?>"></script>

在 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:

<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.

凉世弥音 2024-11-13 20:16:43

如果您希望外部 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>');

if you want your external javascript to be dynamic you can make it a php file and give the correct header, example:

<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>');
坐在坟头思考人生 2024-11-13 20:16:43

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 文件(使用 pass_this_variable 代替):

document.write('<IFRAME SRC="'+pass_this_variable+'" WIDTH="300" HEIGHT="400"></IFRAME>');

PHP file (don't forget echo and quoting):

<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):

document.write('<IFRAME SRC="'+pass_this_variable+'" WIDTH="300" HEIGHT="400"></IFRAME>');
弥繁 2024-11-13 20:16:43

你应该修复这一行:
var pass_this_variable = ;

添加 echo 它应该可以工作

You should fix this line:
var pass_this_variable = <?php echo $company['website']; ?>;

Adding echo and it should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文