...

View Full Version : Posting back to same page... <sigh>



rswyatt
06-08-2004, 02:40 PM
Alright, I have a rather lengthy form that I've decided to split into 7 sections (yup, it is lengthy). I don't want the form on 7 different pages, seems incoherent to me...

So, I've decided that through the brilliance of PHP I can simply include the whole form on one page, but using if's to echo out the current part of the page...

The following are the parts of the form that handle whether or not to echo out the current part of the form, I've left out form pieces for space... I guess the main question is... Just how many times can you post back to the same page? it seems to me that after two times... it just kind of pukes and goes bye bye..


//this is form part 1
<?
if (!isset($_POST['pg1']) && !isset($_POST['pg2']) && !isset($_POST['pg3']) && !isset($_POST['pg4']) && !isset($_POST['pg5']) && !isset($_POST['pg6']) && !isset($_POST['pg7'])){ ?>

//Now the form echos out and I have a hidden variable:
<form action = "<? echo $_SERVER['PHP_SELF; ?>" name="needsanalysisPg1" method="post">
<input type="hidden" name="pg1" value="1" />

//and now we submit
<? } ?>

//now we go to form part 2

<? if (isset($_POST['pg1']) && !isset($_POST['pg2']) && !isset($_POST['pg3']) && !isset($_POST['pg4']) && !isset($_POST['pg5']) && !isset($_POST['pg6']) && !isset($_POST['pg7'])){ ?>

//Now the form echos out and I have a hidden variable:
<form action = "<? echo $_SERVER['PHP_SELF; ?>" name="needsanalysisPg2" method="post">

<input type="hidden" name="pg2" value="1" />
<input type="hidden" name="pg1" value="1" />

//and now we submit
<? } ?>

//now we go to form part 3

<? if (isset($_POST['pg1']) && isset($_POST['pg2']) && !isset($_POST['pg3']) && !isset($_POST['pg4']) && !isset($_POST['pg5']) && !isset($_POST['pg6']) && !isset($_POST['pg7'])){ ?>

<form action = "<? echo $_SERVER['PHP_SELF; ?>" name="needsanalysisPg3" method="post">


and so on and so forth and on it goes until we get to part 7.

The problem is that once I've submitted part 1, submitting part 2 often(as I've changed and tweaked) goes back to part 1... sometimes it just goes blank... I can never seem to get past two submissions... any help?

MrShed
06-08-2004, 02:43 PM
erm....could you not try having a session....then assigning a session variable with the number ($pageNo or sumthin) which is readable....will this not be an easier way to do it?

rswyatt
06-08-2004, 02:46 PM
at the top of the page I do have a
session_start();

So, are you saying that I should:

<input type="hidden" name="$_SESSION['pg1']" value="1">

Like that?

MrShed
06-08-2004, 02:49 PM
no. hang on... *looks in trusty php guide*

k sumthing like this:


session_start();
session_register(pageNo);


Then to read the variable you use something like:


$pageNo = $_SESSION[pageNo];


Hope this helps...might do might not!

rswyatt
06-08-2004, 02:52 PM
Alright, this part makes sense... I would much rather use a session... the problem is... where am I getting the $pageNo from? I have to have some way that when they click "next" it sets page to 2 then 3 etc


no. hang on... *looks in trusty php guide*

k sumthing like this:


session_start();
session_register(pageNo);


Then to read the variable you use something like:


$pageNo = $_SESSION[pageNo];


Hope this helps...might do might not!

MrShed
06-08-2004, 02:58 PM
right erm....hmmm. Have sumthing like....wen it starts the first time(so in your code for the first page u want to display) $pageNo=0.

Then in some part of the script where it runs each time u load the page have: $pageNo++;

that should work shouldn't it?


Actually hang on just thought about that, here it is in a bit more together fashion :p



if(!isset $_SESSION[pageNo])
{
$pageNo = 1;
}
else
{
$pageNo ++;
}

session_register(pageNo);


Think that'll work....something along those lines anyway. Have it near the top of your code, or in a common bit of code anyways

MrShed
06-08-2004, 03:05 PM
session_start();
if (!isset($_SESSION[pageNo])
{
$pageNo = 1;
}
else
{
$pageNo = $_SESSION[pageNo];
$pageNo++;
}

session_register(pageNo);

//assuming you are using different files for each page
//if you aren't, just use a switch statement or something

include 'page'.$pageNo.'php';



Here is a bit of code I've just written anyways....should work. That's how I would do it anyhow! :o

Nightfire
06-08-2004, 03:34 PM
session_register(pageNo) isn't doing anything. You might wanna change the code to


<?php
session_start();
$pageNo = $_SESSION['pageNo'];
if (!isset($pageNo)){
$pageNo = 1;
}else{
$pageNo++;
}

switch ($pageNo){
case 1:
// first form here
break;

case 2:
// second form here
break;

case 3:
// third form here
break;
}
?>

Probably a better way to do it but as I've not done this for 2 months I can't remember everythin :p

MrShed
06-08-2004, 03:38 PM
youve gotta do the register bit for the variable to be passed with the session variables...havent you??? how does it get passed otherwise?

Nightfire
06-08-2004, 03:54 PM
Decided to test my code and glad I did. Here's a working version


<?php
session_start();

if (!isset($_SESSION['pageNo'])){
$_SESSION['pageNo'] = 1;
}else{
$_SESSION['pageNo']++;

}

switch ($_SESSION['pageNo']){
case 1:
// first form here
echo 1;
break;

case 2:
// second form here
echo 2;
break;

case 3:
// third form here
echo '3';
break;
}
?>

You don't need session_register since around version 4.2.0. The super global $_SESSION does all of that now. session_register only works if the register_globals are on, whereas the super global $_SESSION will work if the globals are on or off. http://www.php.net/session-register for more info

MrShed
06-08-2004, 04:02 PM
so....if youve got a session, are all the variables used in the session variable??

Nightfire
06-08-2004, 04:09 PM
Only variables you put into the session variable are entered. In this case it's the number 1 if the session doesn't exist, or the number in the session incremented by 1 if it does exist.

This line $_SESSION['pageNo'] = 1; is starting the session variable with the value of 1.

MrShed
06-08-2004, 04:15 PM
oh ok so i take it u mean that now you just bypass the register thing because you just put them in the array directly? Sorry if i seem dumb im very tired!!!! :p

rswyatt
06-08-2004, 04:22 PM
Well, I've solved part of the problem now... The first part of the form is showing solely... I've coded as per your example... Now, if this makes any diff... I'm posting back to the same page... we're not going to 7 different .php pages, all the same so my form action is
<? echo $_SERVER['PHP_SELF']; ?>

So, on submission of part one... it remains in part one.

I have before the form:
<? switch ($pageNo){
case 1: ?>

Then the form stuff..

Then after part one form:

</form>
<? break;
case 2:
?>

And then part 2 form... and so on... Also, each form is named something different... do my form names have to be the same for all 7 forms?




session_register(pageNo) isn't doing anything. You might wanna change the code to


<?php
session_start();
$pageNo = $_SESSION['pageNo'];
if (!isset($pageNo)){
$pageNo = 1;
}else{
$pageNo++;
}

switch ($pageNo){
case 1:
// first form here
break;

case 2:
// second form here
break;

case 3:
// third form here
break;
}
?>

Probably a better way to do it but as I've not done this for 2 months I can't remember everythin :p

MrShed
06-08-2004, 04:45 PM
Here is my working test code...ignore the session_destroy bit u wont need that, it was just to make sure it was working. Just use this code directly apart from the session_destroy() and the html bit at the bottom....just put in the stuff in the switch cases that u need to and it shud go!



<?php
session_start();

if (!isset($_SESSION['pageNo'])){
$_SESSION['pageNo'] = 1;
}else{
$_SESSION['pageNo']++;
}


switch ($_SESSION['pageNo']){
case 1:
echo "1";
break;

case 2:
echo"2";
break;

case 3:
echo "3";
break;

default:
session_destroy();
}
?>
<html>
<body>
<form name="form1" method="post" action="test.php">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

Spookster
06-08-2004, 04:50 PM
Is this a typo?



<? echo $_SERVER['PHP_SELF; ?>


because that should be



<? echo $_SERVER['PHP_SELF']; ?>

rswyatt
06-08-2004, 04:53 PM
Yeah, that was a typo.

rswyatt
06-08-2004, 05:19 PM
Here we go... Problem was solved! Thanks to ALL for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum