views:

183

answers:

5

Language: PHP

I have a form which asks users for their educational details, course details and technical details. When the form is submitted the page goes to a different page to run processes on the information and save parts to a database. HOWEVER(!) I then need to return the page back to the original page, where having access to the original post information is needed.

I thought it would be simple to copy (by value) the multi-dimensional (md) $_POST array to $_SESSION['post']

session_start(); 
$_SESSION['post'] = $_POST;

However this only appears to place the top level of the array into $_SESSION['post'] not doing anything with the children/sub-arrays.

An abbreviated form of the md $_POST array is as follows:

Array
(
    [formid] => 2
    [edu] => Array
        (
            ['id'] => Array
                (
                    [1] => new_1
                    [2] => new_2
                )

            ['nameOfInstitution'] => Array
                (
                    [1] => 1
                    [2] => 2
                )

            ['qualification'] => Array
                (
                    [1] => blah 
                    [2] => blah
                )

            ['grade'] => Array
                (
                    [1] => blah
                    [2] => blah 
                )

        )

    [vID] => 61
    [Submit] => Save and Continue
)

If I echo $_SESSION['post']['formid'] it writes "2", and if I echo $_SESSION['post']['edu'] it returns "Array". If I check that edu is an array (is_array($_SESSION['post']['edu])) it returns true. If I echo $_SESSION['post']['edu']['id'] it returns array, but when checked (is_array($_SESSION['post']['edu]['id'])) it returns false and I cannot echo out any of the elements.

How do I successfully copy (by value, not by reference) the whole array (including its children) to an new array?

THANKS EVERYONE FOR RESPONDING - UPDATE
The issue was caused my irrelevant quote marks in the naming of fields in my form.

+1  A: 

You can use serialize () to convert the array to a string and either pass it by post or store it in the session.

I've noticed funny behaviour before in saving an array to $_SESSION in PHP. Sometimes if you make a reference and use that it's easier, for example:

$session =& $_SESSION['post'];

$session = array ();

Don't ask why though :)

Also, to expand on what haim evgi said in the comment, wrap the print_r in <pre> tags such as:

echo "<pre>";
print_r ($array);
echo "</pre>";

to make the array output readable.

DCD
+2  A: 

I can't tell why this wouldn't work

session_start(); 
$_SESSION['post'] = $_POST;

On the 2nd page, do you call session_start() before accessing $_SESSION?

You could place this directly under session_start() to help debug.

var_dump($_SESSION);
alex
Yes I had got session_start() called. The error was caused by quote marks in the name of the field names. Thanks for responding :)
Simon R
A: 

If you use echo in arrays, PHP will print "Array" instead of the array contents. Use print_r instead.

Your assignment only looks like it its by reference. Internally (to preserve memory) it is a reference until you change $_SESSION['post']. From that point onward, you will have two separate variables.

chiborg
+2  A: 

if this is your real print_r output, then you have some unwanted chars in your array keys.

Array
(
    [formid] => 2
    [edu] => Array //<--- $_SESSION['post']['edu']
        (
            ['id'] => Array // <---- $_SESSION['post']['edu']["'id'"]
Rufinus
Thanks Rufinus - in the form the name I'd assigned to the field contained the quote marks - I have taken these out and now it seems to work properly now!
Simon R
Thanks for responding :)
Simon R
+1  A: 

DCD is on the right track.

Generally, I would store a serialized version of the form in the session and then unserialize it when I needed to use it.

To store it:

$_SESSION['post'] = serialize($_POST);

To retrieve it:

$post = unserialize( $_SESSION['post'] );
cabuki