Jump to content

$_SESSION[], adding values with foreach


tefuzz

Recommended Posts

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";
}

Link to comment
https://forums.phpfreaks.com/topic/155172-_session-adding-values-with-foreach/
Share on other sites

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'] . " ";
?>

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?

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

    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.

    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!  :D 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.