Mohd.Zafranudin Mohd.Zafranudin - 4 months ago 10
PHP Question

Passing Variable to other PHP without refreshing

I am new to PHP. I have these 3 files :


  • index.php

  • functions.php (to organize functions)

  • header.php



I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:

header.php

<!Doctype html>
<html lang="en">

<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>

<body>
<div class="header">

<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->


I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.

functions.php

<?php

function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}

?>


Now, how do I allow this index.php to have custom page title instead of the default given by header.php?

Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?

I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.

I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php

EDIT :

Here I included my index.php

<?php
require 'functions.php';
?>
<?php
get_header();
?>

<table>
<h3>What we learned</h3>

<ul>
<li><a href="sample">Syntax</a> </li>
<li><a href="sample">Variables</a> </li>
<li><a href="sample">Code Flow</a> </li>
<li><a href="sample">Arrays</a> </li>
<li><a href="sample">Superglobals</a> </li>

</ul>
</table>

<?php get_footer(); ?>

Answer

in the functions.php

function get_header(){
    if (file_exists('header.php'))
      require 'header.php';
    else echo "There is an error retrieving a file";   
}

in the header.php, and in the balise title you call the session parameter

<!Doctype html>
<html lang="en">

<head>
    <title>
     <?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else  'Learn PHP'; ?>
    </title>
</head>

<body>
<div class="header">

    <h1>Learn PHP</h1>
    <p>Your library of PHP learning!</p>
    <hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->

and in the index.php

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is the welcome Page'; 
    require 'functions.php'; 
    get_header(); 
?>

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>
    </ul>
</table>

<?php get_footer(); ?>

and in another-page.php

<?php 
    session_start(); 
    $_SESSION['title-page'] = 'this is an another Page'; 
    require 'functions.php'; 
    get_header(); 
?>

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>
    </ul>
</table>

<?php get_footer(); ?>