【问题标题】:iframe design changing back to old design on page reloadiframe 设计在页面重新加载时变回旧设计
【发布时间】:2016-01-27 17:30:11
【问题描述】:

我按照@SequenceDigitale.com 的答案here 设置了一个外部iframe here。一切都很好,但是当我对表格数据进行排序或在该 iframe 中搜索时,页面会重新加载,但会出现旧设计。这是为什么?
代码
我正在使用此代码从外部资源中获取数据

<?php

$content = file_get_contents('http://www.exhibition-directory.com/expostars/index.php/');
$content = str_replace('</title>','</title><base href="http://www.exhibition-directory.com/expostars/" />', $content);
$content = str_replace('</body>','<link rel="stylesheet" href="http://www.informatixtech.com/expostars/wp-content/themes/hotstar-child/hotstar-child/custom.css" type="text/stylesheet" /></body>', $content);
$content = str_replace('</head>','<link rel="stylesheet" href="http://www.informatixtech.com/expostars/wp-content/themes/hotstar-child/hotstar-child/custom.css" type="text/stylesheet" /></head>', $content);
echo $content;

?>

这是我的 iframe 代码

<?php
                echo '<iframe id="iframe" src="http://www.informatixtech.com/expostars/search-page.php" name="Stack" height="1200" frameborder="0" scrolling="auto" width="100%"></iframe>';?>

【问题讨论】:

  • 因为当您提交表单时它会转到exhibition-directory.com/expostars/index.php 而不是您的脚本,所以您需要代理所有请求
  • @MichailStrokin 任何提示如何代理,我不是太专家。我只是不想浪费我的全部工作。谢谢
  • 表单是通过其他站点的ajax提交,还是通过默认浏览器表单提交?您肯定需要链接代理,以便每个链接都指向您的服务器以获取从外部站点提取的新版本以进行呈现
  • @charlietfl 没有它的简单请求,没有 ajax
  • 我现在添加一个新答案

标签: php jquery html css iframe


【解决方案1】:

表单使用 $_GET 变量,因此您可以执行类似的操作

$content = file_get_contents('http://www.exhibition-directory.com/expostars/index.php?'.http_build_query($_GET));

以便将所有传递给表单的变量附加到获取的 URL

添加这个以便表单提交到您的服务器:

$content = str_replace('<form name="searchForm" action="index.php" enctype="multipart/form-data"  method="get">','<form name="searchForm" action="http://www.informatixtech.com/expostars/search-page.php" enctype="multipart/form-data"  method="get">', $content);

更新您的 CSS,使其不依赖于 form[action=index.php]

更新: 添加这个以便重写链接:

$content = str_replace("location.href='index.php?","location.href='http://www.informatixtech.com/expostars/search-page.php?", $content);

再替换一个:

str_replace('<div class="sc-button-reset"><a href="index.php','<div class="sc-button-reset"><a href="http://www.informatixtech.com/expostars/search-page.php', $content);

【讨论】:

  • 实际上它不会起作用,因为您依赖于展览目录上托管的其他资源
  • 它现在不起作用,因为服务器返回的是空白页面而不是结果页面,你能用你正在使用的确切代码更新要点吗?
  • 抱歉,又打错了那个 URL :) 您需要使用自己的 URL (informatixtech.com/expostars/search-page.php) 而不是展览目录 :)
  • 在你的 CSS 中使用类似 form[name='searchForm'] 的东西而不是 form[action='index.php'] 这样它看起来仍然不错
  • 它确实按预期工作,只需更新 CSS 使其看起来不错
【解决方案2】:

在您在 iframe 中抓取并返回的页面中,将点击处理程序添加到所有需要重新加载的链接。

使用 jQuery

$(function(){
    $('a').click(function(){
        var otherSiteHref = encodeURIComponent(this.href);
        var yoursSiteProxyUrl ='/path/to-your-php?url=' + otherSiteHref ;
        this.href = yoursSiteProxyUrl ;
    });
});

然后在服务器解码$_GET['url],从远程站点获取新的 html 并发送它。

对于表单,在您的 php 中设置一个控制器方法并将操作更改为指向您的代理

$('#searchForm').attr('action', '/your-search-proxy.php');

您需要向其他站点发出 cURL 请求以获取搜索表单。

【讨论】:

  • 这可能是一个更好的方法,但没有 JS 就行不通
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2023-03-31
  • 1970-01-01
  • 2023-04-01
  • 2014-09-23
  • 2010-12-24
相关资源
最近更新 更多