php中的动态html页面

时间:2011-11-20 14:14:17

标签: php apache

我刚接触php并且我尝试使用php处理动态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");
?>

有人帮忙,因为这个错误正在向后拉

4 个答案:

答案 0 :(得分:1)

您应该避免使用@&amp;使用例如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");
?>

对于一些额外的功劳,请查看switch 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");
?>

答案 1 :(得分:1)

错误消息指出数组page中的索引$_GET 定义(即没有?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");
?>

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

答案 2 :(得分:0)

替换此行:

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

这一个:

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

此更改允许您首先检查$ _GET数组中是否存在'page'键,然后(如果它是真的)检查该值是否为“design”。

请勿在声明前使用@。它用于关闭错误消息,但这将很难调试您的应用程序。

答案 3 :(得分:0)

您最有可能使用浏览器在http://localhost/http://127.0.0.1/请求您的网页。 $ _GET是PHP中的全局数组,由使用GET请求方法传递给PHP脚本的变量组成。 像这样:

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

search是一个脚本,?q=php是一个GET请求。 只需将?page=design传递给您的脚本,就可以使用$ _GET数组使用page变量。 因此,您应在浏览器地址中键入http://localhost/index.php?page=design之类的内容。