filmov
tv
Two list boxes with options from MySQL where second list box changes on selection of first list box.

Показать описание
Two dependent list box is a very common requirement as the options of the second dropdown list box is depends on user selection of option of first dropdown list box. For example if you select a country from first drop down list then the second dropdown list will show the list of states of the selected country.
Read more and download source code from here.
We used student table in our tutorial so the first dropdown list box will show the name of the classes available against each row of data. We should show unique class names only as more records will have same class name. This unique class name we can get by using DISTINCT command in SQL.
By using DISTINCT query we will populate first drop down list using a While loop passing through all the rows of record sets.
More on how to read and populate options by using data from MySQL database.
On selection of first drop down list box a JavaScript function reload() will be triggered. Inside this function we will first read the user selected option of the drop down list box. Using this data the page will reload with a name value pair carrying the user selection data.
We will collect this data from the address bar by using GET method in PHP and create a variable.
This variable will be storing the user selected student class , hence we will use this in our SQL to retrieve the student names of that particular class. It is not a good idea to use the variable directly in query, so we will use parameterized query to send the data or parameters separately to the database. We will use MySQLi prepare() function with bind_param() to send string data along with Query. Here is out query with placeholder.
$query2 = "SELECT id,name FROM student WHERE class=? ";
Based on execution of this query we will get a record set containing rows of data matching to the class name passed to the query. We will use one WHILE loop to add the option to the second dropdown list box. Each step of while loop will extract one row of data. After returning one row of data the record pointer will move to next record. When all rows are returned and there is no more rows to return then it will return NULL which is False for the WHILE loop condition and the loop will terminate.
In this script every time we select one option from first drop down list the page will reload by carrying the selected option for the second drop down list to filter. We can use JQuery or Ajax if we don’t want page to reload while passing the value to second list box.
To hold the value of first dropdown list box , you can watch this video
Read more and download source code from here.
We used student table in our tutorial so the first dropdown list box will show the name of the classes available against each row of data. We should show unique class names only as more records will have same class name. This unique class name we can get by using DISTINCT command in SQL.
By using DISTINCT query we will populate first drop down list using a While loop passing through all the rows of record sets.
More on how to read and populate options by using data from MySQL database.
On selection of first drop down list box a JavaScript function reload() will be triggered. Inside this function we will first read the user selected option of the drop down list box. Using this data the page will reload with a name value pair carrying the user selection data.
We will collect this data from the address bar by using GET method in PHP and create a variable.
This variable will be storing the user selected student class , hence we will use this in our SQL to retrieve the student names of that particular class. It is not a good idea to use the variable directly in query, so we will use parameterized query to send the data or parameters separately to the database. We will use MySQLi prepare() function with bind_param() to send string data along with Query. Here is out query with placeholder.
$query2 = "SELECT id,name FROM student WHERE class=? ";
Based on execution of this query we will get a record set containing rows of data matching to the class name passed to the query. We will use one WHILE loop to add the option to the second dropdown list box. Each step of while loop will extract one row of data. After returning one row of data the record pointer will move to next record. When all rows are returned and there is no more rows to return then it will return NULL which is False for the WHILE loop condition and the loop will terminate.
In this script every time we select one option from first drop down list the page will reload by carrying the selected option for the second drop down list to filter. We can use JQuery or Ajax if we don’t want page to reload while passing the value to second list box.
To hold the value of first dropdown list box , you can watch this video
Комментарии