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

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

 

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

    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.

Link to comment
Share on other sites

    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

Link to comment
Share on other sites

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();?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.