tefuzz Posted April 22, 2009 Share Posted April 22, 2009 I have a multi part form, and each step I am validating the previous step. I am storing all the values into a session, and i thought this was a way to do it, but my echo just gives me blank lines... $fields = array("firstname", "lastname", "month", "day", "year", "address1", "address2", "email", "telephone"); foreach ($fields as $field) { $_SESSION[$field] = $_POST[$field]; echo $_SESSION[$field] . "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/ Share on other sites More sharing options...
ILMV Posted April 22, 2009 Share Posted April 22, 2009 Shouldn't it be: $_SESSION[$field] = $fields[$field]; Edit: No that doesn't seem right, can you explain more why you have $fields=array()? ILMV Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816270 Share on other sites More sharing options...
WolfRage Posted April 22, 2009 Share Posted April 22, 2009 You are confusing variables. $_POST is already an array, but if you did not submit a post form request then it will be empty. There fore echo is nothing. Also you need to make an array for your session like so. <?php $fields = array("firstname", "lastname", "month", "day", "year", "address1", "address2", "email", "telephone"); foreach ($fields as $field) { $_SESSION['fields'][]= $field; } var_dump($_SESSION['field'] . " "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816273 Share on other sites More sharing options...
tefuzz Posted April 22, 2009 Author Share Posted April 22, 2009 You are confusing variables. $_POST is already an array, but if you did not submit a post form request then it will be empty. There fore echo is nothing. Also you need to make an array for your session like so. <?php $fields = array("firstname", "lastname", "month", "day", "year", "address1", "address2", "email", "telephone"); foreach ($fields as $field) { $_SESSION['fields'][]= $field; } var_dump($_SESSION['field'] . " "; ?> I have an IF statement checking to see if the form has been posted via a hidden input, this code is executed only after the form is submitted...how to i add the $_POST values to the $_SESSION array then with that? Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816277 Share on other sites More sharing options...
taquitosensei Posted April 22, 2009 Share Posted April 22, 2009 It looks like the fields array are keys in the post array. Try doing print_r($_POST); at the top of your page to make sure your post data looks right, make sure all the keys are there, check upper/lower case etc.. Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816279 Share on other sites More sharing options...
ILMV Posted April 22, 2009 Share Posted April 22, 2009 If you want to put the post variables in the $_SESSION why are you looping through $fields? foreach($_POST as $key=>$value) { $_SESSION[$key]=$value; } Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816284 Share on other sites More sharing options...
tefuzz Posted April 22, 2009 Author Share Posted April 22, 2009 It looks like the fields array are keys in the post array. Try doing print_r($_POST); at the top of your page to make sure your post data looks right, make sure all the keys are there, check upper/lower case etc.. here's what comes out...I only entered firstname, lastname, month, day, and year Array ( [step1] => step1 [token] => 353cefcd1509a11e67b8196efb41284849eef876e280b4.76260405 [firstname] => dsvcasdv [lastname] => asdvasdvdsv [month] => Feb [day] => 4 [year] => 2005 [add1] => [add2] => [city] => [state] => [zip] => => [tel] => [submit] => Next ) dsvcasdv asdvasdvdsv Feb 4 2005 Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816287 Share on other sites More sharing options...
tefuzz Posted April 22, 2009 Author Share Posted April 22, 2009 If you want to put the post variables in the $_SESSION why are you looping through $fields? foreach($_POST as $key=>$value) { $_SESSION[$key]=$value; } good question...for some reason I hadn't thought of just going directly to the $_POST Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816289 Share on other sites More sharing options...
WolfRage Posted April 22, 2009 Share Posted April 22, 2009 Exactly you looped through this array that you created called $fields, and thus that is why I believed you had confused $_POST with $fields. But if you test my script it will work. If you want to loop through $_POST then just replace that with $fields in your foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816291 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 $fields = array("firstname", "lastname", "month", "day", "year", "address1", "address2", "email", "telephone"); foreach ($fields as $field) { $_SESSION[{$field}] = $_POST[$field]; echo $_SESSION[{$field}] . "\n"; } Try that. Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816296 Share on other sites More sharing options...
tefuzz Posted April 22, 2009 Author Share Posted April 22, 2009 Exactly you looped through this array that you created called $fields, and thus that is why I believed you had confused $_POST with $fields. But if you test my script it will work. If you want to loop through $_POST then just replace that with $fields in your foreach loop. I remember what it was! I was originally going to use $fields as $required, and pass it through a validation function but decided against it , I guess I just kept going with it without realizing and worked it into the $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816305 Share on other sites More sharing options...
tang Posted April 22, 2009 Share Posted April 22, 2009 Keep your $fields array and do it that way. If you use tefuzz's code then anyone will be able to set any session variable on your system to whatever they like. EDIT: Your original code looks fine. Are you calling session_start();? Quote Link to comment https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/#findComment-816331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.