SOLVED: Self Submitting Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

SOLVED: Self Submitting Form

Post by JakeJ »

I posted on this before but I deleted it. I think it might have been confusing so I'm starting it again.

I have a form, index.php which checks to see what $body_include should be before any html is output.

It works fine when it first loads. It loads test.php as $body_include first and the the input fields and button display as they should. Then, when I click the button, based on the code in test.php, it SHOULD change $_SESSION['submit'] to test2.php and then when index.php loads again, it should change $body_include to 'test2.php'

What is happening though is that after clicking the button, I have to click it AGAIN in order to send it to the second page. I really do not understand this behavior. If I put $_SESSION['submit'] without a conditional statement, it redirects right away.

*Note: when I echo out $body_include in the body of the form, it return 'test2.php' like it should but apparently, even though $body_include has been reassigned, it's still loading 'test.php'. But it's only doing that if I use the conditional testing in test.php as you'll see below. I am tearing my hair out over this. It's very frustrating.

Here's the files:

Code: Select all

//index.php
<?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>&nbsp;</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</FORM>
</body>
</html

Code: Select all

<?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>";   
       
        //this, without any checks on it will redirect the page to test2.php when index.php reloads.
        //obviously, I comment this out in order to test the IF statement below.
        $_SESSION['submit'] = 'test2.php'; 
       
        //This must already be set since I clicked the button, so why doesn't it behave the way the above statement does?
        //with this code, when the form reloads, I have to click submit an extra time in order to get it to redirect.
        If (isset($_POST['submit'])) { 
                $_SESSION['submit'] = 'test2.php';
        }
?>
Last edited by JakeJ on Mon May 17, 2010 9:28 pm, edited 1 time in total.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Self Submitting Form

Post by JakeJ »

Any takers?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Self Submitting Form

Post by flying_circus »

Which file is which? I assume the first is index.php, what is the second?

Sounds like an order of operations issue.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

SOLVED: Self Submitting Form

Post by JakeJ »

I solved the problem.

I had to split processing in to two files and do the processing BEFORE I checked for $_SESSION['submit'] for the 2nd page load.

Because I'm going to have many files, I also had to make a dynamic file name system so the includes know whether it's going to be including test.php or test_a.php. Here are the new files:

Code: Select all

//index.php
<?php
session_start();


$main_suffix = '.php';
$sub_suffix = '_a.php';
If(isset($_SESSION['root_file'])){
	$process = $_SESSION['root_file'].$sub_suffix;
	include($process);
}

$header_include = './includes/mainstyle.php';



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

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>&nbsp;</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</FORM>
</body>
</html>
and

Code: Select all

//test.php
<?php
	//could just as easily be html only but I will be including some other things. 
        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>";             
?> 
and the file that only does processing:

Code: Select all

//test_a.php
<?php

If (isset($_POST['submit'])) {
                $_SESSION['submit'] = 'test2';
        }
        
?>
Post Reply