Matt
Matt

Reputation: 115

Re-Posting $_POST arrays

I have a script that previews entered data prior to user acceptance of same, it requires the form data to be re-posted so it can be finally processed, some of the data is in array form & I looked for an efficient function to perform that which I couldn't find, so I have created this:

    function repost_array ($value, $key, $mkey) {
            echo draw_hidden_field($mkey.'[' . $key . ']', htmlspecialchars(stripslashes($value)));
    }   
    /* Re-Post all POST'ed variables */
    reset($_POST);
    while (list($key, $value) = each($_POST)) if (!is_array($_POST[$key])) echo draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
    reset($_POST);
     while (list($key, $value) = each($_POST)) if (is_array($_POST[$key]))  array_walk_recursive($_POST[$key], 'repost_array', $key);

Please advise if there is a better way or if I have missed anything (I`ve not tested how deep the recursive function will work).

Upvotes: 1

Views: 78

Answers (1)

Webeng
Webeng

Reputation: 7133

You can repost the values, though I recommend using other alternatives like the $_SESSION super global, something similar to:

<?php
session_start();//session start has to be at the very top of your page!
?>

<!-- ... your html code  -->

<?php 

// ... your php code...

$_SESSION['key'] = $_POST['key']; 

And from there, you can use $_SESSION['key'] anywhere in your code to obtain the stored value (as long as the session is active, which depending on your configurations is usually around 30 minutes but can be made to be longer, like 7 days for instance)

Upvotes: 1

Related Questions