Welcome to WebmasterWorld Guest from 54.205.170.21

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Creating a back button for php multi page session() form

   
4:48 am on Feb 7, 2011 (gmt 0)



100% lost on this. I have a php script that creates a multi page form from a single page of script. All the examples I have found online use browser history -1 or brings back to some #*$!X.com/...html page. Neither of these will work, so any advise would be appreciated.

<?php

//Turn on sessions
session_start();

//Find what stage to use
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['stage'])))
{
$stage = 1;

}
else
{
$stage = (int) $_POST['stage'];
}

// Save any submitted data
if ($stage > 1)
{
foreach ($_POST as $key => $value)
{
$_SESSION[$key] = $value;
}
}
if ($stage == 1)
{
?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>

First Name:
<input type='text' name='first_name' />

<input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
<input type='submit' name='submit' value='Next' />
</form>
<?php
}
else if ($stage == 2)
{
?>
<form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>

Favorite Color:
<input type='text' name='color' />

<input type='hidden' name='stage' value='<?php echo $stage +1 ?>'/>
<input type='submit' value='Done'/>
</form>
<?php
}
else if ($stage == 3)
{
?>
Hello <?php echo $_SESSION['first_name'] ?>
Your favorite color is <?php echo $_SESSION['color'] ?>
<?php

}
?>
5:33 pm on Feb 8, 2011 (gmt 0)



UPDATE:

Thought to add another hidden input with a $stage - 1

<input type='hidden' name='stage' value='<?php echo $stage +1 ?>'/>
<input type='submit' value='Done'/>
<br />
<input type='hidden' name='stage' value='<?php echo $stage -1 ?> '/>
<input type='submit' value='Back'/>


But that is not working, just does what the last hidden value='' says to do.
6:27 pm on Feb 8, 2011 (gmt 0)



Hi,

I'm not 100% sure if you could use this process but I thought it was worth a post. I did something like what I think your describing with this.

When the page is first loaded I counted the page load with a counter and write it to a flat file with:

$count_my_page = ("counter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);

then echo the page hit of 1 into a hidden field in the form like:

<input type="hidden" class="input" name="rowval" value= <?php echo $hits[0]; ?> />

then pick up the posted count with:

$rowval = $_POST['rowval'];

Then you've got something to manipulate with for your if statements like:

if ($rowval >= 1) { etc etc

You can also manipulate in different ways for other uses with other counters like:

This would reset the counter for another use:

if ($rowval == 5) {
$resetcounter = ("counter.txt");
$reset = file($resetcounter);
$res = fopen($resetcounter , "w");
fputs($res , $reset=4);
fclose($res);
$rowval = 4;
}

Or if its another submit button you can use something like this:

if (isset($_POST['submit']) and ! empty ($_POST['submit'])){
strip_tags(mysql_real_escape_string($_POST['submit'], $mysql));
// this row count is counter number reset by 1 to match previous script run for submit above.
$rowval = $rowval -1;
// this counter resets the counter in counter.txt to match the submitted number.
$resetsubmit = ("counter.txt");
$resetsub = file($resetsubmit);
$ressub = fopen($resetsubmit , "w");
fputs($ressub , $resetsub = $rowval);
fclose($ressub);
}

You could of course change $rowval to $stage and you could probably do the above without writing to the flat file but its another value to use if needed.

Hope it helps or perhaps spark an idea.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month