【问题标题】:Picking up html and include into php page提取 html 并包含到 php 页面中
【发布时间】:2015-07-12 08:13:36
【问题描述】:

我是 PHP 的业余爱好者。我正在编写一个名为“index.php”的文件,并将其内容放入一个名为“Built.html”的文件中

Index.php

<body> 
 <?php include 'built.html';?>
</body> 

Built.html

<div id=mainContent> 
  <p>I love 'Hermione Granger.'</p>
</div> 

我正在使用 xampp,并且在转到“Localhost/myfolder/index.php”(我的文件在这个文件夹中)时,我无法从 html 文件中获取到 index.php 文件中的内容。我做错什么了吗?

回答:

我搞定了。我认为“Xampp”主机存在一些问题。我重新启动它,并完成了文件。 Thnx,"vihan" 提供了更多选项来获取包含在我的 php.ini 中的外部文件。虽然,它与我的问题不太相关,但我很感激!

【问题讨论】:

  • 这两个文件在同一个目录吗?并且您发布的一个文件以B 开头,但包含中的名称以b 开头?
  • 您确定 index.php 文件在您的服务器上运行吗?
  • 为什么不用一些js呢?
  • 我认为你应该试试这个:&lt;?php include('built.html') ; ?&gt;
  • @Abhishek - 错了。如果你给了,你的回答一定是反对票。

标签: javascript php jquery xampp


【解决方案1】:

不确定你得到了什么,iframe 可能会更加严格。如果您只是想获取文本或其他内容,请不要使用它:

<div id="mainContent">
    <iframe src="built.html"></iframe>
</div>


php

PHP,您可能需要确保路径实际上是正确的:

include __DIR__ + '/built.html';

另外,我知道这对某些人有用:

include('built.html');


或 JavaScript:
var xhr = (new window.XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP"));

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        document.getElementById('mainContent').innerHTML = xhr.responseText;
    }
};

xhr.open('GET', 'built.html', false);
xhr.send();

【讨论】:

  • 请注意iframe 的工作方式与仅包含 HTML 完全不同。在大多数情况下,这绝对不是我推荐的解决方案。
  • 我认为 PHP 中已经为此提供了一个条款...它应该可以与 include('built.html') 一起正常工作。对于像 include 这样简单的事情,我认为使用 JavaScript 是不可行的
  • 不,我想用 PHP 来做。我想知道为什么我的 div 没有加载。 ;(
猜你喜欢
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
相关资源
最近更新 更多