更改包含的php文件中的文本

时间:2017-07-26 21:00:27

标签: javascript php html

我有一个HTML / PHP页面,我从数据库中检索数据。 使用PHP包括我有一个head.php和footer.php。在我的index.php中,我在开始和结束时将它包含在文件中。 但现在我遇到了一个问题,因为根据从index.php(或任何其他文件)中的数据库中检索到的信息,我想更改页面的<h1></h1>,但该部分位于head.php中。

在已包含的.php文件中更改<h1>内的文本的最简单方法是什么? 编辑:PHP有可能吗?我可以使用HTML,还是必须使用javascript?

head.php

<html>
<head></head>
<body>
//A lot of code that makes the frame of my page
<h1 id="pagetitle"></h1>
//more code that makes the frame of my page

的index.php

include 'head.php';

//..retrieve information from database
//..change text inside <h1> inside head.php


include 'footer.php';

footer.php

</body>
</html>

1 个答案:

答案 0 :(得分:0)

一旦PHP解析并包含了您的标题代码的内容,它就完成了交易。 PHP以后不会重新解析它。您需要一些JS或JQuery返回并替换代码中的值,例如,替换为ID。

例如(index.php中的JS):

document.getElementById("pagetitle").innerHTML = "<?php echo $myTitleVariable ;  ?>";