Creating a drop down list box using data from MySQL tableWe 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.
The list box is here
Selecting only male or female studentsBy using where condition we can restrict the list to only show male students. The SQL part is here.
Or we can display only female
Pre selection of option in dropdown list boxWhile 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.
In this code we are populating the listbox one time while the page is loading. So this is not conditional listing. This loading can be changed so listbox will populate based on some user action.
Interlinked Multiple dropdowns in formWe 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.
Editing the data of dropdown listWe have used student table in this example. This script can be integrated with record edit script to change the table data.
HTML 5 and DatalistDatalist is added in HTML 5 . Like dropdown list users can select options from available choice and in addition to that they can add their own text.
How to create student table?Use the SQL dump of student table given here, create database ( if not there ) and then create the table using your phpMyAdmin.
I am getting database errorOpen config.php file or read more here
Enter correct login details , userid and password.
My dropdown listbox is not populating.
Number of User Comments : 15