user3273001
user3273001

Reputation: 5

Php Navigation issue

I got this line of code right here:

<?php include_once('pages/component/header.php') ?>
<?php include_once('pages/component/nav.php') ?>
<!-- BODY -->
<?php 
$action=$_GET['status'];
switch($action)
{
     case 'about': include_once "pages/about.php";break;
     case 'portfolio': include_once "pages/portfolio.php";break;
     case 'contact': include_once "pages/contact.php";break;
     default : include_once "pages/default.php";break;

}
?>
<?php include_once('pages/component/footer.php') ?>

but when I'm surfing in to the page on WAMP localhost, I get this error saying:

Notice: Undefined index: status in C:\wamp\www\index.php on line 5

Does any body know why this occurs?

It works just fine when I upload it on my FTP.

Upvotes: 0

Views: 92

Answers (4)

Armin
Armin

Reputation: 1756

The problem is that you are not first checking if there is a $_GET['status'] variable. For example, if you go to your URL like this: http://localhost/index.php OR http://localhost/ then there is no $_GET variable set. With the code you have, it will always work as long as you have at least ?status= in the url. You must set that variable if you are going to use it.

It is best to first check to see if there is a $_GET variable in the URL. This should fix your problem:

<?php include_once('pages/component/header.php') ?>
<?php include_once('pages/component/nav.php') ?>
<!-- BODY -->
<?php 
$action= (isset($_GET['status'])) ? ($_GET['status']) : ('');
switch($action)
{
     case 'about': include_once "pages/about.php";break;
     case 'portfolio': include_once "pages/portfolio.php";break;
     case 'contact': include_once "pages/contact.php";break;
     default : include_once "pages/default.php";break;

}
?>
<?php include_once('pages/component/footer.php') ?>

Upvotes: 1

Jakub Matczak
Jakub Matczak

Reputation: 15656

It says that $_GET['status'] simply doesn't exists. It's not an error it's a notice.

Difference between you local environment and remote one is in configuration. This notice still exists, but it's just not displayed. You can configure it via display_errors and error_reporting variables in PHP.ini or alternatively configure them on runtime in PHP.

To solve it you can just check if it exists before you use it.

$action=isset($_GET['status'])?=$_GET['status']:'';

or

$action=empty($_GET['status'])?=$_GET['status']:'';

empty() function also checks if it exists ( isset() functionality) and also check if it's not empty.

Upvotes: 0

mehmetseckin
mehmetseckin

Reputation: 3107

It still produces the notice on your web server, but your web server configuration doesn't allow it to be shown on the screen.

Get rid of it by using isset().

if(isset($_GET["status"]) {
   $action = $_GET['status'];
} else {
   $action = "";
}

Upvotes: 0

helion3
helion3

Reputation: 37361

It means $_GET['status'] is not defined. This refers to the query parameters, so index.php?status=something

You can check for it first, like if( isset($_GET['status'])){

Upvotes: 1

Related Questions