Retaining array values after submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denniss
Forum Newbie
Posts: 9
Joined: Wed Sep 09, 2009 9:10 am

Retaining array values after submit

Post by denniss »

I am wondering whether there is a way to retain array values after submission. This is what I have tried so far and it is not working. The array contains objects that I created on my own.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="set" id="set" value="Set Variables" />
<input name="storeArr" name="storeArr" type="hidden" value="<?php echo serialize(htmlentities($arrVars)); ?>" />
</form>

and after submission i do.

$arrVars = unserialize(html_entity_decode(stripslashes($_POST['storeArr'])));
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Retaining array values after submit

Post by requinix »

It should be

Code: Select all

<?php echo htmlentities(serialize($arrVars)); ?>
and

Code: Select all

if (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) $_POST["storeArr"] = stripslashes($_POST["storeArr"]);
$arrVars = unserialize($_POST['storeArr']);
If the array has actual objects then their definitions (ie, the "class Foo {...}" code) must be given or included in the new script. PHP does not store the full definition of a class - just the data - so if it doesn't know how to reconstruct the object it won't work as you want. (Using __autoload will work.)
denniss
Forum Newbie
Posts: 9
Joined: Wed Sep 09, 2009 9:10 am

Re: Retaining array values after submit

Post by denniss »

the definition is included in the new script by using require_once(); plus I am working only under 1 script here by using $_SERVER['PHP_SELF']
Post Reply