Jump to content

$_SERVER['PHP_SELF'] and dynamic includes


JakeJ

Recommended Posts

This problem is absolutely KILLING ME.

 

I have index.php and it's a form with action=<?php echo $_SERVER['PHP_SELF']; ?>

 

I want to do everything inside of index.php using includes.

 

I have some code that determines that name of the include that goes in the body of the page. This works fine when I first load the page since it's either looking for the results of a variable or goes to a static page name.

 

The include that goes in the body of the page, contains the inputs for the form. When I submit the form, I want it to do some processing and error checking and either return the errors (which works just fine) or redirect back to index.php but with a different include in the body.

 

The example that I've got forces me to submit twice in order for the redirect to happen if I do any conditional checking and assign the next page based on that.

 

I thought about posting my sample code that isn't working but I'm so turned around in my head now, I don't know which way is up. Starting with a fresh example from someone else is bound to give me some perspective on the matter.

 

So how would you do this?

 

Thanks!

 

 

Link to comment
Share on other sites

Here's my code:

 

What happens is, when index.php loads and $_SESSION['submit'] is not set, it loads test.php. That works great. When I click the button on the form, the code in test.php checks to see if submit is set and sets $_SESSION['submit'] and sets that to 'test2.php'. But then when the form reloads, I have to click submit AGAIN in order for it to actually go to test2.php. I don't know why. Since $_SESSION['submit'] has been set, why doesn't my logic at the top of index.php read it and set $body_include to the right form and load it appropriately?

 

<?php
session_start();
$header_include = './includes/mainstyle.php';

if(isset($_SESSION['submit'])) {
                $body_include = $_SESSION['submit'];
}
Else {
        $body_include = "test.php";
}

include ($header_include);
?>
</head>

<body class="twoColFixLtHdr">
        <FORM NAME = "MAINFORM" ACTION=<?php echo $_SERVER['PHP_SELF']; ?> METHOD=POST runat="vdaemon">
        <!-- begin #header -->
        <?php include_once("/includes/incl_header2.php");
        ?>
        <!-- end #header -->
       
        <!-- begin #sidebar -->
        <div id="sidebar1">
        <?php include ("/includes/incl_sidebar.php");
        ?>
        </div>
        <!-- end #sidebar -->

        <!-- end #sidebar1 -->
       
        <!-- begin #main content -->
        <div id="mainContent">
        <?php
        echo $body_include;
        include($body_include);
        #$process =1;
       
        ?>

        </div>
        <!-- end #mainContent -->
       
        <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
        <br class="clearfloat" />
  <div id="footer">
    <p> </p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</FORM>
</body>
</html>

 

//test.php
<?php
        Echo "Please log in<br>
        Username: <input type='text' name='username' value='' /><br />
        Password: <input type='password' name='password' value='' /><br /><br />
        Log In <INPUT ID='submit' NAME='submit' TYPE=IMAGE BORDER=0 SRC='../images/wiz_edit.png' VALUE='login'><p>";   
       
        If (isset($_POST['submit'])) {
                $_SESSION['submit'] = 'test2.php';
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.