user1247412
user1247412

Reputation: 647

PHP and mysql multiple select dropdown

I am trying to populate drop down from mysql table. I am able to populate it successfully. But when I try to retrieve the data after submitting the form, I am not able to retrieve selected values. Can anyone help me on this?

$authorDB=new AuthorDB();
$myArr =$authorDB->retrieveAuthors();
echo '<tr>
<td rowspan="3"><div style="position: relative;">Author</div></td>
<td>
<select name="selAuthor" id="$selAuthor" multiple="multiple" size="3">';
foreach ($myArr as &$s_author)
{
echo '<option value='.$s_author.'>'.$s_author.'</option>';
         }
'</select>
</td>
</tr>'

enter code here

and after submitting the form

$a_SelectedAuthors[]=$_POST["selAuthor"];
$nAuthors = count($a_SelectedAuthors);
echo '<h1> Count :'.$nAuthors.'</h1>';
for($i=0; $i < $nAuthors; $i++)
{
    echo($a_SelectedAuthors[$i] . " ");
}

Upvotes: 0

Views: 4270

Answers (3)

fourreux
fourreux

Reputation: 125

Check the rest of Your code:

The lines </select> ... closing the select tag might not be printed as there's no echo that should print it. After the one above there's a semicolon!

Maybe that's a typo.

Upvotes: 0

scessor
scessor

Reputation: 16115

Remove the brackets while setting $a_SelectedAuthors after submit:

replace

$a_SelectedAuthors[]=$_POST["selAuthor"];

with

$a_SelectedAuthors=$_POST["selAuthor"];

And add them into the name-attribute of the select:

<select name="selAuthor[]" id="$selAuthor" multiple="multiple" size="3">

Upvotes: 1

btantlinger
btantlinger

Reputation: 813

Because you are submitting multiple values as an array, you need to use selAuthor[] as the value of the name attribute.

<select name="selAuthor[]" id="$selAuthor" multiple="multiple" size="3">

Upvotes: 2

Related Questions