0

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] . " ");
}
1
  • You should give us more detail ? Does you $_POST['selAuthor'] is empty ? If yes, did you checked you didn't used the GET method for your form ? Commented Mar 7, 2012 at 7:19

3 Answers 3

2

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">
Sign up to request clarification or add additional context in comments.

2 Comments

it is working but if i have option as 'xyz pqr' it is just giving xyz.why is that?
no quotes around your value. '<option value="'.$s_author.'">' should work as expected
1

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">

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.