Page 1 of 1

How to pass arrays from one document to the other?

Posted: Thu Nov 24, 2011 7:30 pm
by TheDumbNerd
If i had a value that the user had entered into my form on a form page, how do you pass that value onto another page where it is dispayed in a table? Like it gets validated on one page, lets called it the process page. If the values that the user hadentered into the form are legal, then you get redirected to a main page where thedata that the userJUST ENTERED is displayed in a table.

Re: How to pass arrays from one document to the other?

Posted: Thu Nov 24, 2011 11:21 pm
by Livengood
You load into a string and put each array in it together with a : and then split the : back into an array.

Re: How to pass arrays from one document to the other?

Posted: Fri Nov 25, 2011 3:44 am
by Hermit TL
Someone correct me if I'm wrong (as I have never tried it) but can't an array be stored in $_SESSION['value']?
I use $_SESSION for passing single variables between scripts but I thought it could be used for arrays as well.

Re: How to pass arrays from one document to the other?

Posted: Fri Nov 25, 2011 3:59 am
by mikeashfield

Code: Select all

<?php
// begin the session
session_start();
 
// create an array
$array_names=array('mike', 'sarah', 'jake', 'liam');
 
// put the array in a session variable
$_SESSION['names']=$array_names;
 
// a little message to say we have done it
echo 'Putting array into a session variable';
?>
Now that you have the array $array_names in a session variable called $_SESSION['names'] we can have a look through the array as we choose.

Code: Select all

<?php
// begin the session
session_start();
 
// loop through the session array with foreach
foreach($_SESSION['names'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>