Passing array to function
| From: | Bill Hudspeth | Date: | Tue, 13 Aug 2002 16:10:13 +0000 |
| Subject: | Passing array to function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-112354@lists.php.net to get a copy of this message | ||
I am trying to write a function that builds a multiple-choice drop down
list. As such, I need to specify an array in the name attribute of the
<select> tag. I am trying to pass a parameter to the function which gets
used as this name attribute, but seem to be having problems with the square
brackets. In the example below, I would like to submit the $elementname
variable to a function, but can't get it to take the '[' and ']'
characters.
My resultant $_POST[] variable is empty. Any hints?
HARD-CODED VERSION
echo "<select name='cgroup_1[]' multiple size='5'>";
echo "<option value = '$chon_group_a[1]'
selected>$chon_group_a[1]</option>";
echo "<option value = '$chon_group_a[2]'>$chon_group_a[2]</option>";
echo "<option value = '$chon_group_a[3]'>$chon_group_a[3]</option>";
echo "<option value = '$chon_group_a[4]'>$chon_group_a[4]</option>";
echo "</select>";
//the $chon_group_a is a locally defined array that does not seem to be
causing any problems
DESIRED FUNCTION VERSION
$elementname="cgroup_1[]";
$elementsarray=$chon_group_a;
PrintOptionBox($elementname,$elementsarray);