jvr
jvr

Reputation: 25

How to change page content in PHP?

I have a master page that has a content division. Depending on which menu item is clicked, I would like to change the content accordingly.

Example: If "About us" is clicked then I would include the about us content in the content div.

Upvotes: 0

Views: 5447

Answers (1)

Konstantin Ivanov
Konstantin Ivanov

Reputation: 410

On PHP without JS. You will need to pass a page identifier by POST or GET methods

Click on such a link on a page will send a GET request to masterpage called index.php with variable 'content_id' of 'about' value

<a href="index.php?content_id=about">About Us</a>

In the PHP script you will need to get by PHP this variable from web server. like

<?php
    $content=$_GET['content_id']; // _GET is web server array with get variables
?>

in the content section of your master page there could be an 'if' or 'case' construction which will include a required content

<?php
    if ($content == 'about'){
    echo "About We are the Best";
    // you could include a file or data from database here
}else{
    echo "Some other page";
}
?>

p.s. PHP has a lot of already mature frameworks with templates, no need to write one from scratch :-)

Upvotes: 1

Related Questions