Submitting PHP Arrays

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
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Submitting PHP Arrays

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.">";
}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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));
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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);
Post Reply