Details of a record after selection from dropdown listboxTitle: Mango : Fruits
After selecting a record by using two dropdowns we can display full details of the selected record.
This is part 2 of the tutorial of two interlinked dropdown listbox .
Details: Mangoes are juicy and high in Nutritional value Here are the files used in the script dd.php : main front end script which displays two selections
ddck.php : Back end PHP script to collect matching options for second list. Returns data as JSON string
ddck2.php : Back end PHP script receives subcat_id and returns the full detials of the matching record to display.
dd-submit.php : Receives the selected options after submission of the form.
config.php : Database connection string , used by dd.php and ddck.php files. Read more on MySQLi Database connection
sql-sump.txt : Use this sql dump to create tables used in this script.
dd.php : Showing the list box and jQueryThis file populates the first drop down list ( category ) by taking records from category table. For this it connects to database and collects all the records and add them as options to category list box.Category name is displayed as Text part and cat_id is taken as options. Here is the code to populate the 1st list box ( note: jQuery is not used )
Now we have seen in our previous listbox tutorial how to collect user selection of a list box. Here also once the category is selected by user the change function get fired.
Inside this function we will collect the user selection and store it a JavaScript variable cat_id . If required we can also collect text part of the selection, this part is commented in the present script however you can use them instead of selected options.
After getting the variable now we will send it to backend script (ddck.php) and get new set of sub categories for our second dropdown , before that we will remove the existing options if any for the second dropdown.
Now we will use get method to post cat_id to backend script ddck.php and get all the matching records. These records are added as option to our second list box with id = sub_category.
When we receive the data for sub category we will get JSON string so we need to loop through and add each option to drop down listbox.
You can read more on how to add option to an existing dropdown list box. Now the above process repeats once the first dropdown of category is changed by user. $('#category').change(function(){ var cat_id=$('#category').val(); $('#sub-category').empty(); //remove all existing options /////// $.get('ddck.php',{'cat_id':cat_id},function(return_data){ $.each(return_data.data, function(key,value){ $("#sub-category").append("<option value=" + value.subcat_id +">"+value.subcategory+"</option>"); }); my_function(); // Show the details of the record }, "json"); /////// }); Backend script ddck.phpThis page receives the posted value of cat_id and collects the matching records from dd2_subcategory table. The output is posted back as JSON string to calling page ( dd.php ) . Here is the complete code.
Getting the selected record details after selection of 2nd dropdown listOnce the 2nd dropdown list option is selected or changed we can pass the subcat_id value to our backend PHP script to get all the details ( field values ) of the record. Here we have used one more table dd2_subcategory_dtl to store details of the record against each subcat_id. You can extend the table with more fields to keep more details against each subcat_id . ( You can download the SQL dump to create your tables available inside ZIP file at the end of this tutorial )
Here we have used my_function() to send the subcat_id to backend script and collect the record details to display. The same function is also called when the 2nd dropdown is populated with data and one of the option is selected ( see the code above inside $('#category').change(function() )
PHP Script at ddck2.phpOur backend PHP script ddck2.php received the subcat_id value and collects the details of the matching record and post it to our main script.How to get details of a single record using MySQLi Finally we display the output of ddck2.php in our main script. dd-submit.phpAfter selecting category and subcategory the user can submit the form. The data is collected and displayed inside dd-submit.php page.
How to Install the script.Open config.php file and enter your database login details.Create tables by using sql-dump.txt file inside phpmyadmin Open dd.php file through your server . This tutorial uses the concepts adding option to list box , posting data using get method and JSON parsing of string to get data. You can further extend to create three dropdown list linking to each other. jQuery Listbox
This article is written by plus2net.com team.
|
Most Popular JQuery Scripts | |
1 | Two dependant list boxes |
2 | Calendar with Date Selection |
3 | Data change by Slider |
4 | Show & Hide element |