【问题标题】:HTML Iframe get src with phpHTML iframe 用 php 获取 src
【发布时间】:2013-02-16 03:52:41
【问题描述】:

我有一个网页,我想在其中显示另一个 html 页面。我使用了 iframe。页面通过 get 过程知道要加载什么。但是我认为这个编码有一个错误......

            <iframe src="
            <?
            $file       = ($_GET['ti'])

            if ($title = '')
                echo "information.html";
                else echo "$file";
            ?>
            "></iframe>

页面将收到的 url 如下所示: http://www.website.com/reference.html?ti=unlimited.html

【问题讨论】:

  • 附带说明,根据您设置 PHP 变量的时间,您可能会遇到服务器/客户端脚本问题,这意味着您可能需要使用一些 JavaScript(可能是 jQuery)来执行 AJAX更改src 属性的脚本。
  • 发生了什么,您希望它做什么?

标签: php html url iframe src


【解决方案1】:

http://www.w3schools.com/php/php_if_else.asp

这是您的 if / else 语法和所有 php 代码。写的不是很好。

<?php

$file = $_GET['ti'];

if ($title = '') {
     echo "information.html";
} else { 
     echo "$file";
}

?>

【讨论】:

  • 是的,代码写得不好,但 If/Else 不是“问题”。如果后面有一行,则不需要括号。是的,这是一个很好的做法,但不是必需的。
  • 没有大括号,if 只会在忽略else 语句后立即执行。至少这是我的理解。
  • 是的,if 语句将执行下一条语句,但即使没有大括号,else 仍会被考虑。 Example
  • 看看你在说什么。读起来还是让我眼睛疼:(
【解决方案2】:

需要分号:

        $file       = ($_GET['ti']);

【讨论】:

    【解决方案3】:

    使用empty(),像这样:

        <iframe src="
        <?
        $file       = ($_GET['ti'])
    
        if (empty($title))
            echo "information.html";
            else echo $file;
        ?>
        "></iframe>
    

    【讨论】:

      【解决方案4】:
      <?php
          $possible = array("information.html", "home.html", "test.html");
          $file = isset($_GET['ti']) && 
                  in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
      ?>
      <iframe src="<?php echo $file;?>"></iframe>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-09
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多