我已经查看了其他文章,在这里有关类似的问题,但无法找到答案。如何将外部php文件的输出放置到页面上的特定位置
编辑我一直在玩这个,我想改变我的问题。
我有一个问题,放置从外部的PHP文件回显的输出。
下面是一些示例代码,以证明我的问题 - 这是我的主文件writephp.php
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div class="allContent">
<?php for($i=0; $i<3; $i++): ?>
<div class="commentBox">
<p> Just a box</p>
</div>
<?php endfor; ?>
<a href="write.php?outputHtml">Php output</a>
</div>
</body>
</html>
现在我可以用CSS中心这样的:
.allContent {
width: 400px;
margin-left: auto;
margin-right: auto;
font-family: Arial;
}
.commentBox {
border: 1px solid black;
width: 400px;
padding: 0px 5px 5px 5px;
margin-top: 5px;
}
所以在HTML文件中的PHP由于php循环在“allConent”div内,所以写出了页面中心的框。
该锚定调用一个外部php文件,将回声一些更多的框。
这个文件看起来是这样的,被称为write.php
(writephp.php
是这是从所谓的主文件):
<?php
if (isset($_GET['outputHtml']))
{
echo "<p class='allContent commentBox'>" . "This is from external PHP" . "</p>";
echo "<p class='allContent commentBox'>" . "This is also from external PHP" . "</p>";
include 'writephp.php';
}
但是从外部PHP输出的呼应是从上面的输出主要文件 - 其实它放在<!doctype html>
标签之前,显然不好。
页面的源代码如下所示:
<p class='commentBox allContent'>This is from external PHP</p
><p class='allContent commentBox'>This is also from external PHP</p>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div class="allContent">
<div class="commentBox">
<p> Just a box</p>
</div>
<div class="commentBox">
<p> Just a box</p>
</div>
<div class="commentBox">
<p> Just a box</p>
</div>
<a href="write.php?outputHtml">Php output</a>
</div>
</body>
一般来说,我的问题是“我怎么把HTML从外部php文件的任何地方我想在页面上未来?”
**在单个HTML页面上,你永远不能拥有多个具有相同ID的元素**。您究竟如何加载新内容? –
外部php文件使用'include'writephp.php''这是主页面。它的工作原理,但我知道我不应该有多个具有相同ID的元素,这就是为什么我想知道正确的方法是什么。 – user2250892
什么是writephp.php? –