切换到不同的PHP包括

时间:2013-05-16 18:09:38

标签: php php-include

我已经使用HTML一段时间了,我刚刚开始使用PHP 我创建了一个简单的页面,其中包含一个使用PHP include函数的菜单。该菜单有效,但我想知道如何设置一个原始包含文件,当单击其中一个菜单按钮时,该文件被替换。 这是我的代码:

<html>
<head>
    <title>
        Test
    </title>
</head>
<body> 
    <?php 
        include 'header.php';
    ?>
    <form action="" method="post">
        <input type="submit" name="Home" value="Home" />
        <input type="submit" name="AboutUs" value="About Us" />
        <input type="submit" name="Games" value="Games" />
        <input type="submit" name="Pages" value="Pages" />
    </form>
    <?php
        if (isset($_POST['Home']))
        {
            include 'home.php';
        };
        if (isset($_POST['AboutUs']))
        {
            include 'au.php';
        };
        if (isset($_POST['Games']))
        {
            include 'games.php';
        };
        if (isset($_POST['Pages']))
        {
            include 'pages.php';
        };
    ?>
</body>
</html>

我希望在页面首次加载时包含home.php,然后在按下其中一个菜单按钮时将其替换。 我怎么能用PHP做到这一点?或者有更好的方法吗?

3 个答案:

答案 0 :(得分:6)

这可能是一个干净的解决方案:

<form action="" method="post">
    <input type="submit" name="page" value="Home" />
    <input type="submit" name="page" value="About Us" />
    <input type="submit" name="page" value="Games" />
    <input type="submit" name="page" value="Pages" />
</form>
<?php
    switch($_POST["page"]){
        case "About Us":
            include 'au.php';
            break;
        case "Games":
            include 'games.php';
            break;
        case "Pages":
            include 'pages.php';
            break;
        default:
            include 'home.php';
            break;
    }

答案 1 :(得分:0)

所以,这将检查,如果未设置$_POST,则加载home.php else,如果已设置则继续

    if (!isset($_POST)){ // if post is not set, show home.php
           include 'home.php';
        }else if (isset($_POST['AboutUs'])){
            include 'au.php';
        }else if (isset($_POST['Games'])){
            include 'games.php';
        } // and you can keep going like that

答案 2 :(得分:0)

这样您就可以选择...

<html>
 <head>
  <title>
    Test
  </title>
 </head>
 <body> 
  <?php 
    include 'header.php';
  ?>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="menuForm">
      <input type="submit" name="submit" value="Home" />
      <input type="submit" name="submit" value="About Us" />
      <input type="submit" name="submit" value="Games" />
      <input type="submit" name="submit" value="Pages" />
     </form>
  <?php
    switch ($_POST["submit"]) {
        case 'Home': {
            include 'home.php';
                            break;
        }
        case 'About Us': {
            include 'au.php';
                            break;
        }
        case 'Games': {
            include 'games.php';
                             break;
        }
        case 'Pages': {
            include 'pages.php';
                            break;
        }
        default: {
            include 'home.php';
                            break;
        }
    }
      ?>
 </body>
</html>

希望这有帮助。