ajbr
ajbr

Reputation: 1

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

Upvotes: 0

Views: 1764

Answers (3)

Orangepill
Orangepill

Reputation: 24655

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

Upvotes: 6

Josh Balcitis
Josh Balcitis

Reputation: 498

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.

Upvotes: 0

anon
anon

Reputation:

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

Upvotes: 0

Related Questions