Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过url传递javascript变量并解码为php变量_Php_Javascript_Json_Iframe - Fatal编程技术网

通过url传递javascript变量并解码为php变量

通过url传递javascript变量并解码为php变量,php,javascript,json,iframe,Php,Javascript,Json,Iframe,我发现这个小脚本允许用户在他们的站点上嵌入一个简单的javascript代码,以显示来自我的站点的iframe。这工作得很好,但是我试图通过他们嵌入的代码传递1个变量,并在我这端将其设置为php变量 我提前道歉,我对javascript不太了解!对不起 用户将嵌入的示例代码(小部件类型代码) 这是在我这边生成iframe的代码 // Set path to the iframe file var filePath = 'http://www.mysite.com/public/iframe.

我发现这个小脚本允许用户在他们的站点上嵌入一个简单的javascript代码,以显示来自我的站点的iframe。这工作得很好,但是我试图通过他们嵌入的代码传递1个变量,并在我这端将其设置为php变量

我提前道歉,我对javascript不太了解!对不起

用户将嵌入的示例代码(小部件类型代码)


这是在我这边生成iframe的代码

// Set path to the iframe file
var filePath = 'http://www.mysite.com/public/iframe.php';
// Setup the iframe target
var iframe='<iframe id="frame" name="widget" src ="#" width="100%" height="1"   
marginheight="0" marginwidth="0" frameborder="no" scrolling="no"></iframe>';
// Write the iframe to the page
document.write(iframe);

var myIframe = parent.document.getElementById("frame");
// Setup the width and height
myIframe.height = 350;
myIframe.width = 960;

myIframe.src = filePath;
// set the style of the iframe
myIframe.style.border = "1px solid #999";
myIframe.style.padding = "8px";
//设置iframe文件的路径
var filePath=http://www.mysite.com/public/iframe.php';
//设置iframe目标
var-iframe='';
//将iframe写入页面
文件编写(iframe);
var myIframe=parent.document.getElementById(“框架”);
//设置宽度和高度
myIframe.height=350;
myIframe.width=960;
myIframe.src=文件路径;
//设置iframe的样式
myIframe.style.border=“1px solid#999”;
myIframe.style.padding=“8px”;

最终目标是将rel属性id=1分配给php变量以供进一步使用。我四处查看了很多,似乎json_decode是答案,但似乎没有任何效果。

不要将
id
作为
rel
属性传递,只需将其直接添加到url:

<script language="JavaScript" src="http://www.mysite.com/public/iframe.js?id=1"></script>
这是一个缩写
if
,用于检查以确保id已添加到url。这与:

if(array_key_exists('id', $_GET) {
    $id = $_GET['id'];
} else {
    $id = null;
}

我很确定rel属性不是iframe标记中使用的东西。这通常用在带有rel=“nofollow”的标记中,这样搜索引擎就不会跟踪该链接

我还没有试过,但也许你可以在嵌入代码中打个问号后发送参数。像这样:

<script language="JavaScript" src="http://www.mysite.com/public/iframe.js?id=1"></script>

然后,您的PHP代码只在GET请求中查找它

<?php
echo $_REQUEST['id'];
?>


当然,这假设您的接收脚本是用PHP编写的。因为您显示了一个.js扩展名,所以我不确定PHP代码是否正在执行?另外,我不能100%确定您可以在javascript嵌入中传递这样的参数。

在您的javascript中添加以下内容以查找脚本标记本身:

var allScripts = document.getElementsByTagName('script'),
currentScript = allScripts[allScripts.length - 1 ];
然后,您可以使用
rel
属性值或解码
src
属性


顺便说一句,当以异步方式添加脚本元素时,这可能并不总是起作用,在这种情况下,您必须检查
src
是否与您的文件名匹配,或者检查
rel
也许

您可以在iframe上使用rel,尽管我不确定为什么会这样做-它经常用于各种微格式的标记。你可以像这样在url中传递一个参数。但我击败了你们:我试着像你们在url中说的那样传递它,但它不起作用。我假设问题是嵌入代码调用一个javascript文件,该文件调用一个php文件,而php文件需要变量,而不是.js文件。
<?php
echo $_REQUEST['id'];
?>
var allScripts = document.getElementsByTagName('script'),
currentScript = allScripts[allScripts.length - 1 ];