2011-11-20 110 views
0

即时通讯新的PHP和IM尝试用PHP 一切似乎都OK的动态HTML页面的工作,但是当我试图让网页动态 错误显示了这样动态HTML页面

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

我检查了它在网络上,有人坚持使用这个(@)在$前面它工作 虽然当我尝试点击导航栏的设计按钮,我得到这个错误

在请求URL这台服务器。推荐页面上的链接似乎是错误或过时的。请通知该页面的作者关于错误。

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

有人帮助,因为这个错误被向后拉

+1

['@'](http://php.net/manual/en/language.operators.errorcontrol.php)取消错误信息。你真的不应该使用它。有很少的情况下你不能避免使用它,但这不是其中之一。 – rid

回答

0

替换此行:

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

这一个:

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

这种变化让您先检查'page'键存在于$ _GET数组中,然后(如果它是真的)检查值是否是“设计”。

请勿在语句前面使用@。它用于关闭错误消息,但这会使您很难调试应用程序。

+0

感谢球员第一个错误已经处理,但即时通讯仍然看到这一点,当我点击导航栏上的设计按钮,我看到这个错误消息,“在这台服务器上找不到请求的URL。引用页面上的链接似乎是错误或过时请通知该页面的作者关于错误“ – thequantumtheories

+0

@thequantumtheories尝试在其他页面使用相同的标准,在那里你得到的错误。 –

1

如果变量被设置befor例如使用isset()

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

而对于一些额外的信贷看开关case语句作为脚本增长的更清洁的使用,您应该避免使用@ &检查:

<?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"); 
?> 
+0

永远不要只用'@来压制可以处理的警告!这不像是当你不再看到它时,问题就消失了,你知道:) – PeeHaa

+0

@PeeHaa不能同意更多,你有时候在视图中使用抑制器来隐藏未使用的变量,而不是检查每个变量。 –

+0

1个词语:yuk! :) – PeeHaa

1

错误消息表示,阵列$_GET在索引page defin ed(即没有?page=xxx)。

那么当没有页面传递给脚本时,你想要做什么?

你可以用isset()检查,如果一个变量被设置:

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

顺便说一句,你不应该使用@从而抑制所有警告!有很多原因;)