Log in

View Full Version : Help Needed: Multi SELECT item & Arrays



krisjohnson
08-27-2009, 10:52 PM
I'm trying to take the input from a <SELECT> Form item that has the MULTIPLE option turned on.

Once I can do this properly - I plan to populate that list with databse values - but I need to be able to process the users selections in the resulting page call.


print("<select name=subcategory[] multiple=multiple >");

I have a simple list of items and I want the user to be able to select more than one item.

I created my FORM variable as an array - so that the $_POST would process this as an array. I know the $_POST item can return an array - but how do you access that information on the other side?

All examples that I seen really do not address how to move the FORM variable back into a PHP array that can be used.

Basic FORM item:



<?php

print("<form action=./chk_value.php method=post name=newitem>");
print("<fieldset>");
print("<legend class=formTitle> Select A Value </legend>");
print("<P class=ptight>");
print("<label for=subcategory>Product Subcategory</label>");
print("<select name=subcategory[] multiple=multiple >");
print("<option value=1>Apple</option>");
print("<option value=2>Apricots</option>");
print("<option value=3>Bananna</option>");
print("<option value=4>Blueberry</option>");
print("<option value=5>Cherry</option>");
print("<option value=6>Cranberry</option>");
print("<option value=7>Figs</option>");
print("<option value=8>Grapes</option>");
print("<option value=9>Oranges</option>");
print("<option value=10>Strawberry</option>");
print("</select>");
print("</P>");

print("<P>");
print("<input name=add_button type=submit value=Save class=formText>");
print("</P>");
print("</fieldset>");
print("</form>");

?>



At this point - I'm just trying to see the that the values are in my PHP variable in the page that process it - chk_value.php

Here's all the code in the <BODY> tag (pretty simple at this point):


<?php
$subcategory_value = array();

# Get need key value SUBCATEGORY ID
print("The size of the Subcategory array is -> ". sizeof($_POST['subcategory']) . "<BR>" );

for ($i=0;$i<=count($_POST['subcategory']);$i++){
$subcategory_value[] = $_POST['subcategory'];
}


foreach ($subcategory_value as $key => $value)
{ echo $key." - " . $value . "<BR>";
}


?>



I suspect that this code


$subcategory_value[] = $_POST['subcategory'];

in the FOR LOOP is causing the problem.

So when "chk_value.php" executes - How do I properly move the FORM variable captured into a PHP array variable that I can use????

forum_amnesiac
08-29-2009, 07:43 AM
In my current painkiller induced haze this is my suggestion.

The [] in the SELECT create an array that is passed as an array to PHP.

Try this

$subcategory_value=$_POST['subcategory'];

for ($i=0;$i<=count($subcategory_value);$i++){
echo $i." - " . $subcategory_value[i] . "<BR>";
}


I haven't tested it but I think it's on the right track, perhaps somebody with a clearer head might be able to help further

krisjohnson
09-01-2009, 01:28 AM
Fourm_Amnesiac:

I actually came up with a solution too.

This would be for the page that process the multi-select field:




$subcategory_value = array();

# Store values from multi-select field
if (is_array($_POST) && (count($_POST) > 1) ){
#
foreach ($_POST['subcategory'] as $key => $value)
{

$subcategory_value[] = $value;
echo $key." - " . $value . "<BR>";
}
#
}

# Display values of assigned array
foreach($subcategory_value as $key => $value) {
echo $key." - " . $value . "<BR>";
}


This was a code sample enroute to a bigger exercise.

I used this sample to help with a page that has a multi-select field.
The data in the field is generated from a database table.

I then used the selected values to perform multiple inserts in the processing page.

Thanks for taking time to answer my question

krj