简体   繁体   English

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

[英]change text inside a included php-file

I have a HTML/PHP page where I retrieve a data from a database. 我有一个HTML / PHP页面,从数据库中检索数据。 Using PHP includes I have med a head.php and footer.php. 使用PHP包括我将head.php和footer.php结合在一起。 And inside my index.php I include this to files at the start and at the end. 在我的index.php中,我将其包含在文件的开头和结尾。 But now I came across a problem, because depending on the information retrieved from the database in index.php (or any other file) I want to change the <h1></h1> of the page, but that part is in head.php. 但是现在我遇到了一个问题,因为根据index.php(或任何其他文件)中从数据库检索到的信息,我想更改页面的<h1></h1> ,但是那部分在头。 PHP。

What is the simplest way to change the text inside <h1> in an already included .php-file? 在已经包含的.php文件中更改<h1>中的文本的最简单方法是什么? EDIT: Is it possible with PHP? 编辑:可以用PHP吗? Can I use HTML, or do I have to use javascript? 我可以使用HTML,还是必须使用JavaScript?

head.php 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 的index.php

include 'head.php';

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


include 'footer.php';

footer.php footer.php

</body>
</html>

Once PHP has parsed and included the contents of your header code, it's a done deal. 一旦PHP解析并包含了您的标头代码的内容,那就完成了。 PHP isn't going to re-parse it later. PHP将不会在以后重新解析它。 You'd need some JS or JQuery to go back and replace the value in the code, say, replace by ID. 您需要一些JS或JQuery来返回并替换代码中的值,例如,替换为ID。

For example (JS inside of index.php): 例如(index.php内的JS):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM