2011-11-20 118 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"); 
?> 

順便說一句,你不應該使用@從而抑制所有警告!有很多原因;)