1

I would like to create a multi-select drop down list of countries in my form which lets users choose more than one country and also provides the ability to remove the item they selected, if needed.

I could create a drop down list which extracts the name of the countries from a mysql table, but it lets users only select one country. I searched a lot for multi-select drop down but none of the samples that I have found get data from mysql. They had only a few options which could easily write with option value like How to access the values selected in the multiselect dropdown list in PHP?

This is my code through which only one item can be selected:

<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('imdb');

$sql2 = "SELECT country FROM countries";
$result2 = mysql_query($sql2);

echo "<select name='country'>";
while ($row = mysql_fetch_array($result2)) {
    echo "<option value='" . $row['country'] . "'>" . $row['country'] . "</option>";
}
echo "</select>";
?>
9
  • uh, <select name="country" multiple> You browser couldn't care less where the options in the select came from. it just sees html. Commented Jun 12, 2014 at 15:26
  • @MarcB : thanks, I tested now it looks like multiselect, but still i cannot select more than one, I think because I have to use the $POST method. Commented Jun 12, 2014 at 17:31
  • no. a form doesn't care if it's being submitted via get or post. get just has some limits that post doesn't. for a simple form like this, there is no difference. Commented Jun 12, 2014 at 17:32
  • @MarcB: sorry, one more question: I wrote my php code inside html form, could you please tell me if I write it in a separate file, how can I call it from inside html? Commented Jun 12, 2014 at 17:32
  • @MarcB your comment should be an answer Commented Jun 12, 2014 at 17:33

1 Answer 1

1

Finally, I found what I was looking for. So I share it since might be useful for those with similar question.

First, as Marc said, I had to add multiple like below:

<select name="countrylist[]" multiple="multiple">

and this is the html line that I was searching for :

<option value="<?php echo $row['country'];?>"> <?php echo $row['country'];?> </option>

where country is the name of the related field in my database.

it might be worth saying that in order to insert the result in database (e.g table name = "test", field name = "q5":

<?php
mysql_connect("hostname", "user name", "user password") or die(mysql_error());
mysql_select_db("db name") or die(mysql_error());

$q5 = implode(',', $_POST['countrylist']);
 if(isset($_POST['submit']))
 {       
   $query="INSERT INTO test (q5) values ('". $q5 ."')";

    mysql_query($query) or die (mysql_error() );

   }
?>

It worked for me and hope will be useful for others.

Sign up to request clarification or add additional context in comments.

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.