简体   繁体   English

切换到其他PHP包括

[英]Switching to a different PHP include

I have been working with HTML for a while now and I am just starting out with PHP I have created a simple page with a menu that uses the PHP include function. 我已经使用HTML一段时间了,而我刚开始使用PHP,我已经创建了一个简单的页面,该页面的菜单使用了PHP的include函数。 The menu works but I was wondering how to set an original include file that is replaced when one of the menu buttons is clicked. 该菜单有效,但我想知道如何设置单击菜单按钮之一时要替换的原始包含文件。 Here is my code: 这是我的代码:

<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>

I want it to include home.php when the page first loads then replace it when one of the menu buttons is pressed. 我希望它在页面首次加载时包含home.php,然后在按下菜单按钮之一时替换它。 How can I do this with PHP? 我该如何使用PHP? Or is there a better way? 或者,还有更好的方法?

this might be a clean solution for you: 这可能是一个干净的解决方案:

<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;
    }

So, this will check, if $_POST is not set, load home.php else, if it is set then continue 因此,这将检查,如果未设置$_POST ,则加载home.php其他,如果已设置,则继续

    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

Just so you have options to choose from... 只是为了让您有选择...

<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>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM