William82
William82

Reputation: 89

change text inside a included php-file

I have a HTML/PHP page where I retrieve a data from a database. Using PHP includes I have med a head.php and footer.php. And inside my index.php I include this to files at the start and at the end. 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.

What is the simplest way to change the text inside <h1> in an already included .php-file? EDIT: Is it possible with PHP? Can I use HTML, or do I have to use 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>

Upvotes: 0

Views: 414

Answers (1)

user1678946
user1678946

Reputation:

Once PHP has parsed and included the contents of your header code, it's a done deal. PHP isn't going to re-parse it later. You'd need some JS or JQuery to go back and replace the value in the code, say, replace by ID.

For example (JS inside of index.php):

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

Upvotes: 0

Related Questions