如何通过 iframe 将 php 变量传递给 javascript
how to pass php variable through iframe to javascript
我正在尝试通过 iframe 将 php 变量值传递给 javascript 变量。
所有文件都在我自己的服务器和域上。
这是我的 html 文件:
<html>
<head>
<?php
$userOutput = "bob";
?>
</head>
<body>
<p id="test123"><?=$userOutput?></p>
</body>
</html>
在我的原始页面中,我尝试访问这样的信息:
<iframe id="iframeId" src="http://path/to/file.html"></iframe>
<script>
window.onload = function() {
var iframeDoc = document.getElementById('iframeId').contentWindow.document;
var test = iframeDoc.getElementById('test123').value;
console.log(test);
};
</script>
现在,我确实设法达到了我的内容,而且我之前曾尝试过成功地获取我放入 "file.html" 中的某些输入字段的值,但我似乎无法达到php 变量值("test" 显示为未定义)
您可以 echo
您的变量在 Javascript 变量中,例如
<script>var test = "<?= $variable ?>";</script>
并将变量作为 GET 参数传递给您的 iframe。
<script>
var url = "myIframe.html?var1=" + test;
$('#myIframe').attr('src', url");
</script>
然后您可以使用
检索信息
<script>
function getParamValue(paramName)
{
var url = window.location.search.substring(1); //get rid of "?" in querystring
var qArray = url.split('&'); //get key-value pairs
for (var i = 0; i < qArray.length; i++)
{
var pArr = qArray[i].split('='); //split key and value
if (pArr[0] == paramName)
return pArr[1]; //return value
}
}
</script>
我想感谢@Ozgur bar 在这里的回答。
所以任何包含 php 的东西都需要进入 .php 文件而不是 .html
举个例子:
variableStored.php
:
<html>
<head>
<?php
$userOutput = "Frrrrrrr";
?>
</head>
<body>
<p id="test123">
<?php echo $userOutput; ?>
</p>
</body>
</html>
注意:回显时,最好<?php echo 'something';?>
而不是<?='something'?>
然后让我们说 iframe.html
:
<iframe id="iframeId" src="http://siteurl/variableStored.php"></iframe>
<script>
window.onload = function() {
var iframeDoc = document.getElementById('iframeId').contentWindow.document;
var test = iframeDoc.getElementById('test123').value;
console.log(test);
};
</script>
这将根据需要从 variableStored.php
获取所有内容。
我正在尝试通过 iframe 将 php 变量值传递给 javascript 变量。 所有文件都在我自己的服务器和域上。
这是我的 html 文件:
<html>
<head>
<?php
$userOutput = "bob";
?>
</head>
<body>
<p id="test123"><?=$userOutput?></p>
</body>
</html>
在我的原始页面中,我尝试访问这样的信息:
<iframe id="iframeId" src="http://path/to/file.html"></iframe>
<script>
window.onload = function() {
var iframeDoc = document.getElementById('iframeId').contentWindow.document;
var test = iframeDoc.getElementById('test123').value;
console.log(test);
};
</script>
现在,我确实设法达到了我的内容,而且我之前曾尝试过成功地获取我放入 "file.html" 中的某些输入字段的值,但我似乎无法达到php 变量值("test" 显示为未定义)
您可以 echo
您的变量在 Javascript 变量中,例如
<script>var test = "<?= $variable ?>";</script>
并将变量作为 GET 参数传递给您的 iframe。
<script>
var url = "myIframe.html?var1=" + test;
$('#myIframe').attr('src', url");
</script>
然后您可以使用
检索信息<script>
function getParamValue(paramName)
{
var url = window.location.search.substring(1); //get rid of "?" in querystring
var qArray = url.split('&'); //get key-value pairs
for (var i = 0; i < qArray.length; i++)
{
var pArr = qArray[i].split('='); //split key and value
if (pArr[0] == paramName)
return pArr[1]; //return value
}
}
</script>
我想感谢@Ozgur bar 在这里的回答。
所以任何包含 php 的东西都需要进入 .php 文件而不是 .html
举个例子:
variableStored.php
:
<html>
<head>
<?php
$userOutput = "Frrrrrrr";
?>
</head>
<body>
<p id="test123">
<?php echo $userOutput; ?>
</p>
</body>
</html>
注意:回显时,最好<?php echo 'something';?>
而不是<?='something'?>
然后让我们说 iframe.html
:
<iframe id="iframeId" src="http://siteurl/variableStored.php"></iframe>
<script>
window.onload = function() {
var iframeDoc = document.getElementById('iframeId').contentWindow.document;
var test = iframeDoc.getElementById('test123').value;
console.log(test);
};
</script>
这将根据需要从 variableStored.php
获取所有内容。