Page 1 of 1

Submitting PHP Arrays

Posted: Mon May 30, 2005 9:38 pm
by harrisonad
let's say i have a group of checkboxes with the same name, 'app[]'.
if submitted, a PHP script an receive them as an array, right?

Code: Select all

$appids = $_POST['app'];
What if i want to send it again to another script? do i have to build another form and redistribute these checkbox values?

Code: Select all

foreach($appids as $appid){
echo "<input type=checkbox name='app[]' value=$appid>";
}
or perhaps, make a hidden field with array values...

Code: Select all

echo "<input type=hidden name=appids value=$appids>";
What do you think is the best way to do this?

Posted: Mon May 30, 2005 9:45 pm
by John Cartwright
Looks fine to me.. only thing I would suggest is naming your array elements, so you can later distinguish them from another. Example:

Code: Select all

foreach ($_POST['app'] as $appname => $appid)
{
     echo $appname.": <input type=checkbox name='app[".$appname."]' value=".$appid.">";
}

Posted: Mon May 30, 2005 10:11 pm
by JAM
Thought it would be worth mentioning serialize and base64_encode aswell...

Code: Select all

$appids = $_POST['app'];
$newarr = base64_encode(serialize($appids));
echo '<input type="hidden" name="oldarr" value="'.$newarr.'">';

// leater...
$appids = $_POST['oldarr'];
$newarr = unserialize(base64_decode($appids));

Posted: Tue May 31, 2005 12:11 am
by ol4pr0
Or how about storing it serialized into a temp db.tbl

and then retrieve it with some Unique id or whatever.

Code: Select all

# thanks for this one 
function get_unserialized( $args ) {
	$var = "";
	foreach($args as $postvar){
	$var .= $postvar;
	}
	return $var;
}
#query
$whtvr = unserialize($row->whtvr);

#get it unserialized if needed 
get_unserialized($whtvr);