Multi-page form and sessions.

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
Brian3864
Forum Newbie
Posts: 9
Joined: Mon Aug 16, 2004 10:29 am

Multi-page form and sessions.

Post by Brian3864 »

I have a 4 page form with a summary page at the end before final submittal. I have decided to use sessions in case someone wanted to go back and edit information before submitting the form. I'm using links on the summary page to go back to the desired page to edit. I have come across a couple of problems, 1 of which I figured out a fix but I'm not sure if it's the easiest way. The 2 problems I have come across is,

1. person fills out page 1 and moves to page 2, they then fill out page 2 and realize they need to edit something on page 1 so they hit the back button on the browser. When they edit page 1 and click next, the information on page 2 is no longer there. Is there around this?

2. When the person gets to the summary page and click on a link to edit information on any given page, the info wasn't there at which time I felt pretty stupid in forgetting to set the info according to the session info, so I added some code,

Code: Select all

<?php 
session_start();
header("Cache-control: private");

if (!isset ($_SESSION['domain']))
{
	$_SESSION['domain'] =NULL;
}
?>
and then the input box

Code: Select all

<? print "<input name="Domain" type="text" id="Domain" value="{$_SESSION['domain']}">" ?>
Which works fine, but with all of the inputs, is there an easier way to do this? without having to set every session as null and coding every input box that way?[/u]
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Mabey you could use: session_destroy()
Brian3864
Forum Newbie
Posts: 9
Joined: Mon Aug 16, 2004 10:29 am

Post by Brian3864 »

I don't want to destroy the session, I need the if statement so that if there is no session I don't get errors when creating the input field using the $_SESSION['domain'] when it hasn't be set yet.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

generally.. it's done in a similar way..

Code: Select all

<?php

$fieldnames = array('domain','last_name');
foreach($fieldnames as $fieldname)
 $$fieldname = (isset($_SESSION[$fieldname]) ? $_SESSION[$fieldname] : '');

?>
now you can use $domain or $last_name as the value of each field..
Brian3864
Forum Newbie
Posts: 9
Joined: Mon Aug 16, 2004 10:29 am

Post by Brian3864 »

Thanks for the help, now I have a more complex problem. Not a problem per say, but a question. I have the inputs coded so that if the session exists, it scripts the value into the input, I have numerous drop downs to where if I coded it as

Code: Select all

<? select name="State" id="State"><option value="{$_SESSION['State']}" selected>{$_SESSION['State']}
<option value="Alabama">Alabama
etc.
?>
Whatever state they select will be in the dropdown twice. Bear with me here as I have just picked up php in the last couple of days, but how would I go about scripting it so that the previous selection stored in the session doesn't show up twice in the drop down?

Thanks for all the help.
Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use a [php_man]for[/php_man]/[php_man]while[/php_man]/[php_man]foreach[/php_man] loop to traverse the array of values you want to show in the dropdown(s).. when you find the one where the session variable matches, write "selected"
Brian3864
Forum Newbie
Posts: 9
Joined: Mon Aug 16, 2004 10:29 am

Post by Brian3864 »

Thanks for the help. I ended up using 2 while loops. Have 2 questions though.
1. Is there an easier/cleaner way?
2. I haven't thoroughly tested this, will this code cause any forseeable problems/errors?

This code is just the testing code for now, I will incorporate the session values later.

Code: Select all

<select name="State">
<?
$state&#1111;0] = "Alabama";
$state&#1111;1] = "Alaska";
$state&#1111;2] = "Arizona";
etc
$selected = "Alaska";
$x = 0;
$y = 3;
while ($state&#1111;$x] != $selected) &#123;
print "<option value="&#123;$state&#1111;$x]&#125;">$state&#1111;$x]</option>";
++$x; &#125;
print "<option value="&#123;$state&#1111;$x]&#125;" selected>$state&#1111;$x] </option>";
$x = $x + 1;
while ($x < $y) &#123;
print "<option value="&#123;$state&#1111;$x]&#125;">$state&#1111;$x]</option>";
++$x; &#125;
?>
</select>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

for($x = 0, $y = sizeof($state); $x < $y; $x++)
echo '<option value="' . $state[$x] . '"' . ($state[$x] == $selected ? ' selected="selected"' : '' ) . '>' . $state[$x] . '</option>'."\n";
Post Reply