我可以使用 PHP 将 URL 变量传递给 IFrame 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9511990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:59:09  来源:igfitidea点击:

Can I Pass a URL variable to an IFrame using PHP?

phpiframe

提问by Simon

I've not used PHP much (or at all) before, and I have the following code:

我以前很少(或根本没有)使用 PHP,我有以下代码:

<?php
$val = $_GET['ID'];
echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid='$val'    width='100%' scrolling='vertical'></iframe>";
?>

I think that should be ok to take a URL variable and pass it to an Iframe url...my issue is that when I hit the page this is on instead of it being:

我认为采用 URL 变量并将其传递给 iframe url 应该没问题……我的问题是,当我点击页面时,它是打开的,而不是:

http://sitename.com/whats-on?ID=2

its

它的

http://sitename.com/whats-on/?ID=2

I don't know where that slash before /?ID is coming from - but I believe it is causing my problem - the iframe displaying a page not found message.

我不知道 /?ID 之前的斜杠是从哪里来的 - 但我相信这是导致我的问题 - 显示页面未找到消息的 iframe。

Any advice appreciated.

任何建议表示赞赏。

Thanks

谢谢

Simon

西蒙

回答by FriendlyGuy

iFrames just take a url - and parameters can be embedded in urls just fine.

iFrame 只需要一个 url - 参数可以很好地嵌入到 url 中。

The problem, if I understand the question clearly, is that you're mixing up your quotes:

如果我清楚地理解这个问题,问题在于你混淆了你的报价:

 echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid='$val'
        width='100%' scrolling='vertical'></iframe>";

will be outputted as

将输出为

 <iframe src='sitename.com.au/directory/app/pagename.cfm?memberid=' 21254545' 
  width='100%' scrolling='vertical'></iframe>

where 21254545 is an attribute of the iframe instead of part of the url.

其中 21254545 是 iframe 的一个属性,而不是 url 的一部分。

Assuming that you don't actually need the quotes in the url, change the echo line to:

假设您实际上不需要 url 中的引号,请将 echo 行更改为:

echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid=$val' width='100%' scrolling='vertical'></iframe>";

And it should work.

它应该工作。

回答by Makesh

  • Add http://before sitename.com.au/
  • Change memberid='$val' to memberid=$val'[remove that single quoteon left of $val]
  • 在 sitename.com.au/ 前添加http://
  • 将 memberid='$val' 更改为memberid=$val'[删除$val 左侧的单引号]
<?php
   $val = $_GET['ID'];
   echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid=$val' width='100%' scrolling='vertical'></iframe>";
?>

回答by Warface

Seems that the

似乎

echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid=$val' width='100%' scrolling='vertical'></iframe>";

doesn't work anymore. You have to add the variable like this

不工作了。你必须像这样添加变量

 echo "<iframe src='sitename.com.au/directory/app/pagename.cfm?memberid=".$val."' width='100%' scrolling='vertical'></iframe>";

Since the anwser was from 2012... maybe PHP patched it to be used like that now.

由于 anwser 是从 2012 年开始的……也许 PHP 修补了它现在可以这样使用。