...

View Full Version : Hit a snag.. how to set correct amount of session variables



cyphix
05-16-2004, 06:53 PM
Ok, I am using session variables to carry values through several pages of user interaction, however I have just hit a snag wondering how I can set the correct amount of session variables in the second part.

You see, I already know what fields (form input fields) are on the first page so I can easily set those fields to session variables in the first script; however the second page is dynamic & I will have no idea how many of each field will be there so I can't set them beforehand.

There could be 1 field, there could be 20.

The script will of course know how many there are, but I don't see the benifet in that as you would need to include all possible variables in the script anyway for the script to set them if needed.

Any ideas here?

Thanks!

firepages
05-16-2004, 07:04 PM
assuming your variables are POST ?

just loop through whatever is passed and set the session..

so in each page have something like ...


<?
session_start();
/*post vars to ignore*/
$ignore=array( 'submit' );
foreach($_POST as $k=>$v){
if( !isset( $_SESSION['form_vars'][$k] ) && ! in_array( $k , $ignore ) ){
$_SESSION['form_vars'][$k]=$v;
}
}

print_r($_SESSION['form_vars']);
?>


untested but you see the idea ?

cyphix
05-16-2004, 10:46 PM
Yeah.. they are POST.

Ok I put that in my page but don't really get how it works.

Also, what is the print_r there for? I know what it does, but why are we using it?

firepages
05-17-2004, 06:17 AM
Some variables you don't need carried around like say submit from your submit button ..anything in here does not get added to the session.
$ignore=array( 'submit' ); (and checked with im_array())

then basically we loop through all the POST variables , if they do not already exist in the $_SESSION['form_vars'] array (and they are not part of the $ignore array) we add them to the $_SESSION['form_vars'] array

the print_r() is not required as such its just a visual to show you the state of the $_SESSION['form_vars'] as you go along whilst testing.

so run your forms (with the above at the head of each page) , and on the last page the sesion will have all the unique POST variables that were entered.

using session variables is nice cos you can also then let users go 'back' and refill your form fields from the session data

cyphix
05-17-2004, 01:27 PM
Ok... so how would I refer to each item in the array?



Array (

[input] => value1
[input2] => value2
[input3] => value3
[input4] => value4
[input5] => value5 )


That's the type of output I get using this.

So is the arry stored in $ignore?



$ignore[input1]


..or are we making use of the $v variable?

cyphix
05-17-2004, 01:30 PM
Looking at it again, I assume we use something in the form of this..



$_SESSION['form_vars'][$v]


..or something.

cyphix
05-17-2004, 03:45 PM
I got it figured out heh..



$_SESSION['form_vars']['variable'];


:D

cyphix
05-17-2004, 09:20 PM
Ok, ran into a problem here with this code you gave me..



<?
session_start();
/*post vars to ignore*/
$ignore=array( 'submit' );
foreach($_POST as $k=>$v){
if( !isset( $_SESSION['form_vars'][$k] ) && ! in_array( $k , $ignore ) ){
$_SESSION['form_vars'][$k]=$v;
}
}

print_r($_SESSION['form_vars']);
?>


Some new code of mine that depended on getting these session variables wasn't working & I couldn't work out why so I have been going through everything & found a problem with this code..

It brings the values of the variables from the last page, however, if something already has a current value it will use that as the value rather than the input from the user.

I have some fields where I have some default values in them & some which don't... I outputted the results to the screen & it displays the correct values for each field I didn't supply a default value for but outputs the default value for all boxes I did supply default values no matter what I enter into these fields.

Any help?

Thanks a lot!

firepages
05-17-2004, 09:56 PM
ok , if you want to be able to overwrite the defaults ... change this

if( !isset( $_SESSION['form_vars'][$k] ) && ! in_array( $k , $ignore ) ){

to this

if( ! in_array( $k , $ignore ) ){

in otherwords remove the bit that checks if the value already exists in the session

cyphix
05-17-2004, 11:17 PM
Thanks! Works good!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum