Switching to a different PHP include

谁都会走 提交于 2019-12-25 18:43:02

问题


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. 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. How can I do this with PHP? Or is there a better way?


回答1:


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



回答2:


So, this will check, if $_POST is not set, load home.php else, if it is set then continue

    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



回答3:


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.



来源:https://stackoverflow.com/questions/16594504/switching-to-a-different-php-include

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!