Two linked dropdown list box to select category and subcategory using PHP MySQL & JQuey

preview_player
Показать описание

You have seen how we populate a dropdown list by using options from MySQL database table. Watch the video on this.
We will further extend this concepts to create two dependent dropdown lists. We say they are dependant because the options of the second list will change dynamically based on the selection of first list.
For example if any one select India as country then our second list should show all Indian states as options. If the user changes its selection of country to USA then our second list should show all states of USA.
Download the source code of this script in zip file at the end of the tutorial given at description here.
This is the main script file, displaying list boxes.
There is a similar script in PHP section also , but Advantage of using JQuery is page need not reload. In this script we will first complete the PHP part to collect the records from category table and display them as options of first drop down list.
Create one dropdown list box without any option.
Then collect the option from category table to populate the list box. Check at browser that drop down list is displayed with options taken from database category table.
Once one options is selected from first dropdown list , the change event is triggered and inside this function we will collect the value of selected option i.e the value of cat_id.
var cat_id=$('#category').val();
We will remove all the available options of 2nd dropdown list box subcategory from the previous selection if any.
$('#sub-category').empty(); //remove all existing options
After posting cat_id we will receive one matching subcategory records as Json key value pair. We will hold this key value pair in our variable return_data. Then we will loop through each key value pair and add them as options to subcategory dropdown list box.
Using cat_id the Query is prepared.
$sql="SELECT subcategory,subcat_id FROM subcategory WHERE cat_id=:cat_id";
Using this query the data is collected from subcategory table and one JSon string is prepared using them.
Рекомендации по теме
Комментарии
Автор

Thank You for this wonderful concept, Very easy to understand.

binshafiqdigital
Автор

how to make history in db that user submit?

rayjohnornales
join shbcf.ru