Forum Moderators: coopster

Message Too Old, No Replies

Sessions on Forms

         

HoboTraveler

10:36 am on Jan 20, 2009 (gmt 0)

10+ Year Member



Hi All,

I have a signup form that users get to based on session variables that were set in the previous step.

When the user opens a new tab and gets to the point where the session variables are set again, and then returns back to the signup form in the previous tab, the session variables in the previous tab are overwritten with the ones from the new tab.

Is there a way to prevent session variables in the previous tab from getting overwritten by new tabs?

Thanks

enigma1

12:47 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use a separate array of vars for each tab that it's stored in a session. You must pass a variable either via get, post etc, to identify the tab index. Use the index to access the correct array that holds the vars in the session.

HoboTraveler

2:17 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



Hello,

Do mean something like a two dimensional array? Could you post an example.

Thanks

enigma1

5:28 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




// setup array of tabs and defaults define it before the session starts
array $tab_array(
array(
'key1_1' => 'value1_1',
'key1_2' => 'value1_2',
//.....
),
array(
'key2_1' => 'value2_1',
'key2_2' => 'value2_2',
//.....
),
//etc.............
);

Then you got to check the session make sure it is started, I assume that you have code in place for that to filter spiders etc, so lets say the $session_started is the session flag.


// Initialize tab index to the first tab
if ($session_started == true ) {
if( !isset($_SESSION['tab_info']) ) {
// tab index not in session initialize it.
$_SESSION['tab_info'] = $tab_array;
} else {
// tab_info in session, load it.
$tab_array = $_SESSION['tab_info'];
}
}

// Process tab_index to know user choice, lets assume it is passed from the $_GET array
switch ($_GET['tab_index']) {
case 0:
// Load template for tab-index-1 and vars
$key1 = $tab_info[0]['key1_1'];
$key2 = $tab_info[0]['key1_2'];
break;
case 1:
// Load template and vars for tab-index-2
$key1 = $tab_info[1]['key2_1'];
$key2 = $tab_info[1]['key2_2'];
break;
case 2:
// Load template and vars for tab-index-2
$key1 = $tab_info[2]['key3_1'];
$key2 = $tab_info[2]['key3_2'];
break;
default:
break;
}

During the page processing you may want to update the $tab_array. Its contents will be stored in the session automatically. Also this sample assumes global scope, if you are using functions you need to setup the $tab_array as global.

If you wanted say to update value1_1 you do:
$tab_info[0]['key1_1'] = 'new_value1_1';
And the tab_info will maintain its contents during page transition for the same user.