Page 1 of 1

getting values from a multi-selectable listbox

Posted: Sun Jun 29, 2008 10:31 pm
by psychotomus
I am currently using this code and it doesn't seem to get all the values from the multi-selected listbox, but only one of the values.

Code: Select all

        $cats = '|';
        for($i=0; $i < count($_POST['select']); $i++)
        {
            $cats .= mysql_real_escape_string($_POST['select'][$i]) . '|';
        }
            
        echo $cats;

Re: getting values from a multi-selectable listbox

Posted: Sun Jun 29, 2008 11:08 pm
by Stryks
You haven't shown how the select box is drawn. The reason I say this is that in order for php to access multiple values, the select box needs to be named accordingly.

Code: Select all

<select name="select" multiple>
 
should be
 
<select name="select[]" multiple>
Then the value returned to php will be an array of values selected. Does this help??



Just as a side note, I find foreach() to be far nicer on the eyes then for(). For example ...

Code: Select all

foreach($_POST['select'] as $selected_val) $cats .= mysql_real_escape_string($selected_val) . '|';
Alternately, you could go with something more like ...

Code: Select all

$escaped_values = array();
foreach($_POST['select'] as $selected_val) $escaped_values[] = mysql_real_escape_string($selected_val);
$cats = "|" . implode("|", $escaped_values);
It's entirely up to you though.