Nishant
Nishant

Reputation: 69

How to load content from other page in index page with using url variable

I am trying to lad content from page1.php, page2.php and page3.php with the help of url varibale in my index page.

Here is my index page code

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>
<li><a href="index.php?page=page1.php">page 1</a>page1</li>
<li><a href="index.php?page=page2.php">page 2</a>page2</li>
<li><a href="index.php?page=page3.php">page 3</a>page3</li>
</ul>
<?php
    $page = $_GET['page'];
    $pages = array('page1', 'page2', 'page3');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
?>
</body>
</html>

the index page shows undefined variable $page

Upvotes: 2

Views: 2313

Answers (3)

Parth Chavda
Parth Chavda

Reputation: 1829

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>

change page1.php to page1 beacuse in array there is no page1.php it's only page1.....

<li><a href="index.php?page=page1">page 1</a>page1</li>
<li><a href="index.php?page=page2">page 2</a>page2</li>
<li><a href="index.php?page=page3">page 3</a>page3</li>
</ul>
<?php

you got undefine index because first time when page load $_GET['page'] doesn't exit.you need to check wether $_GET['page'] is set or not

if(isset($_GET['page']))
{
    $page = $_GET['page'];
    $pages = array('page1', 'page2', 'page3');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

here $page only contain page1,page2...else so you need to concate with ".php"

            include($page.".php");
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
}
?>
</body>
</html>

Upvotes: 0

Tintu C Raju
Tintu C Raju

Reputation: 2700

you have to use array('page1.php', 'page2.php', 'page3.php'); or avoid .php extension from url aswell as array and use $page.".php" in include. Also make sure that $_GET['page']; is set

<html>
<head>
</head>
<body>
<h1>Hello there></h1>
<ul>
<li><a href="index.php?page=page1.php">page 1</a>page1</li>
<li><a href="index.php?page=page2.php">page 2</a>page2</li>
<li><a href="index.php?page=page3.php">page 3</a>page3</li>
</ul>
<?php
    $page = isset($_GET['page'])?$_GET['page']:'page1.php';
    $pages = array('page1.php', 'page2.php', 'page3.php');
    if (!empty($page)) {
        if(in_array($page,$pages)) {

            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
?>
</body>
</html>

Upvotes: 1

Matt Oaxaca
Matt Oaxaca

Reputation: 196

I'd recommend using a routing system from a framework.

Aside from that your $_GET variable: index.php?page=page1.php is returning the string "page1.php" which doesn't exist in your $pages array. Add the .php into your $pages array and you should have it working.

Recommendations for routing systems: http://silex.sensiolabs.org/

Uses symfonys routing and request components. Will save you some headaches.

Upvotes: 0

Related Questions