add php variable into <title> html tag

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 229
Location: Holland Rotterdam

add php variable into <title> html tag

I run your Website Mini-Traffic and Pages Access data: ... ss-data_s2

From my mebers page Users.php i don`t get an title from database.
I added simple an <title>, and i`m very supriced i cant add php code into <title>.
in this code it does not show value of $users['name'].
i tried also echo php also not working:

Code: Select all


$id = (int) $_GET['id'];
$sql=mysqli_query($conn,"SELECT * FROM `user` WHERE id='" . $id . "' ");

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<title>Profile View '. $users['name'] .'</title>

function myFunction() {
    var x = document.getElementsByTagName("TITLE")[0].text;
    document.getElementById("demo").innerHTML = x;

Add the php variable into <?php .. ?>, with "echo".
Try this:

Code: Select all

$sql=mysqli_query($conn,"SELECT * FROM `user` WHERE id='" . $id . "' LIMIT 1");
$title ='Defaul Title';
  while($row=mysqli_fetch_assoc($sql)) $title = $row['name'];
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

<title>Profile View <?php echo $title; ?></title>

Similar Topics