Mohd.Zafranudin
Mohd.Zafranudin

Reputation: 454

Passing Variable to other PHP without refreshing

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

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(); ?>

Upvotes: 2

Views: 263

Answers (2)

Walf
Walf

Reputation: 9288

It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.

header.inc

…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…

index.php

<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome

another-page.php

<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content

If you want to use a function, give it parameters.

function get_header($title = 'Some default title') {
    …
}

the included file will have access to the variables in the function's scope.

Upvotes: 1

Fahmi B.
Fahmi B.

Reputation: 798

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(); ?>

Upvotes: 0

Related Questions