thequantumtheories
thequantumtheories

Reputation: 120

dynamic html pages in php

im new to php and im try to work on dynamic html pages with php everything seemed to ok but when i try to make pages dynamically an error shows up like this

Notice: Undefined index: page in C:\xampp\htdocs\myfolder\website\inter.php on line 5

i checked it out on the net and someone insisted on using this(@) in front of $ it worked though when i try to click on the navbar the design button i get this error

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

<?php

include ("includes/header.html");
include ("includes/navbar.html");
if ($_GET['page'] == "design") {
      include ("includes/design.html"); 
}
else {
     include ("includes/home.html");
}  

include ("includes/footer.html");
?>

somebody help because this error is pulling backwards

Upvotes: 0

Views: 1335

Answers (4)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

You should avoid using @ & check if the variable is set befor using e.gisset()

<?php

include ("includes/header.html");
include ("includes/navbar.html");
if (isset($_GET['page']) && $_GET['page'] == "design"){
      include ("includes/design.html"); 
  }else{
     include ("includes/home.html");
}  

include ("includes/footer.html");
?>

And for some extra credit look at the switch case statement as your script grows its much cleaner to use:

<?php
include ("includes/header.html");
include ("includes/navbar.html");

$page=(isset($_GET['page']))?$_GET['page']:'home';
switch($page){
    case "home":
        include ("includes/home.html");
        break;
    case "design":
        include ("includes/design.html");
        break;
    case "otherPage":
        include ("includes/otherpage.html");
        break;
    default:
        include ("includes/404.html");
        break;
}

include ("includes/footer.html");
?>

Upvotes: 1

ComFreek
ComFreek

Reputation: 29424

The error message says that the index page in the array $_GET is not defined (i.e. no ?page=xxx).

So what do you want to do when there's no page passed to the script?

You can check with isset() if a variable is set:

<?php

include ("includes/header.html");
include ("includes/navbar.html");

// $page defaults to an empty string
// If the "page" parameter isn't passed, this script will include "home.html"
$page = '';
if ( isset($_GET['page']) )
  $page = $_GET['page'];

if ($page == "design")
{
      include ("includes/design.html");
}

else // If $page isn't "design" (, "...") or $page is an empty string, include "home.html"!
{
     include ("includes/home.html");
}  

include ("includes/footer.html");
?>

By the way you shouldn't use the @ which suppresses all warnings! There are many reasons ;)

Upvotes: 1

Nemoden
Nemoden

Reputation: 9056

You, most probably, request your page at http://localhost/ or http://127.0.0.1/ using browser. $_GET is a global array in PHP which consists of variables passed to the PHP script using GET request method. Like this:

http://www.google.com/search?q=php

search is a script, ?q=php is a GET request. Just pass ?page=design to your script and page variable will be available using $_GET array. So you should type something like http://localhost/index.php?page=design in your browser address.

Upvotes: 0

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22152

Replace this line:

if ($_GET['page'] == "design") {

with this one:

if (isset($_GET['page']) && $_GET['page'] == "design") {

This change let you to first check if the 'page' key exists in the $_GET array and then (if if it's true) check if the value is "design".

Don't use the @ in front of the statement. It's used to shut down error messages but this will make hard to debug you application.

Upvotes: 0

Related Questions