SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Double Dropdown List Download Script

Creating a drop down list box using data from MySQL table

We can get the options or the list items of a dropdown list box from a mysql table. Here the records we will get from the table will be used to populate the dropdown list box. So the options are dynamically added to the list box, this way we get flexibility in selecting the options. Here we will first populate the list with options directly taking from the table. Then we will restrict the options by adding a WHERE Clause to the sql statement we use for retrieving the data from the table.

For example we are populating the list box with student names at the first stage. Then we will add a restriction and collect only the records of the boys from the table. This example you can extend to any other condition like displaying state list with population more than some value.

In the first stage we will learn how to get the data from MySQL table and then populate the list box. In second stage we will add restriction by using a period button to the list box. For our demo here we will use the student table and you can download / copy the SQL dump file to create your table with student data. The small PHP code we will discuss you can copy and use directly from here.

We are not discussing the connection to mysql part here and we assume connection is open for us. You can see different mysal connection strings used in different scripts here. We will start with the simple SQL select query used to collect the names and ids of the students from the table.

include "config.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT name,id FROM student order by name";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=student value=''>Student Name</option>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id]>$row[name]</option>";
/* Option values are added by looping through the array */
echo "</select>";// Closing of list box

The list box is here

Pre selection of option in dropdown list box

While populating the listbox by taking data from a table , we can keep one option selected. for example the student name 'Big John' should be remain selected by default. For this we will use one if condition check inside the loop and if it matches then we will add 'selected' to that option.

Here is the foreach loop part only to show the change in code required.

foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[id] selected>$row[name]</option>";
echo "<option value=$row[id]>$row[name]</option>";
echo "</select>";// Closing of list box

Interlinked Multiple dropdowns in form

We often found more than one dropdown linked to each other. Like once a country is selected from one dropdown then matching states of that country is shown in second dropdown list.

If a car manufacturer is selected from one dropdown then 2nd dropdown will list all the models of that particular make.

Populating 2nd dropdown list based on selection of 1st one

Editing the data of dropdown list

We have used student table in this example. This script can be integrated with record edit script to change the table data.

Editing and updating records used in dropdown list

The SQL file ( dump ) to create student table with records is here

Managing list box elements by a radio button

Number of User Comments : 6

I want to retrieve data from database to multiple select dropdown list using PHP.so that the user can select multiple items in that dropdown.Can any one help me.

Any help is appreciated.
Thank you for the post. I have tried the code but its not working, The select list is blank. I am trying to come up with a code to select postal codes from the database.
Below is the code;


include "connection.php"; // Database connection using PDO
//$sql="SELECT name,id FROM student";
$sql="SELECT 'office_name','office_code' FROM postalcodes";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=office_name value=''>office_name</option>"; // list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[office_code]>$row[office_name]</option>";
/* Option values are added by looping through the array */
echo "</select>";// Closing of list box


Paul Reay18-07-2014
I'm finding that unless the option is clicked on then its value isn't being stored, so what if i actually want to use the selected value and just tab to next field on my form, at the moment it wont store the value.
For this you need Ajax. By this when you use tab and move away to next field the value will automatically stored without submitting the form. You can also give a message asking user to select first before moving out or submit the form.
thanks so so much
plz send me the 4 dropdown list filter search in php using mysql
Post Comment This is for short comments only. Use the forum for more discussions.
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer