Thursday, September 20, 2018

Populate multiple dropdown lists using Ajax, jQuery, PHP and MySQL BIT UCSC UoM Classes Colombo Sri Lanaka


Some readers often asking me how to generate or populate more than one dropdown list or multiple select boxes using Ajax. So, In this tutorial, I will show you how to generate or populate multiple dropdown lists using Ajax, jQuery, PHP, and MySQL.
Let’s think a situation where the user has to submit a form where he/she has to fill up the form by selecting continent, country, state, and city. Country list is dependent on the selection of continent, state list is dependent on the selection of the country list and so on. All this dynamically generated select or dropdown options are using Ajax so that no page refresh or reload is required.
If you want to populate only one dropdown list based on selection of another dropdown option, please read this article — populate a dropdown list based on selection of another dropdown option using ajax

Database Tables

  1. CREATE TABLE `continents` (
  2. `continent_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `continent` varchar(255) NOT NULL,
  4. PRIMARY KEY(`continent_id`)
  5. )
  6. CREATE TABLE `countries` (
  7. `country_id` int(11) NOT NULL AUTO_INCREMENT,
  8. `country` varchar(255) NOT NULL,
  9. `continent_id` int(11) NOT NULL COMMENT 'continent_id from the continents table',
  10. PRIMARY KEY(`country_id`)
  11. )
  12. CREATE TABLE `states` (
  13. `state_id` int(11) NOT NULL AUTO_INCREMENT,
  14. `state` varchar(255) CHARACTER SET latin1 NOT NULL,
  15. `country_id` int(11) NOT NULL COMMENT 'country_id from the countries table',
  16. PRIMARY KEY(`state_id`)
  17. )
  18. CREATE TABLE `cities` (
  19. `city_id` int(11) NOT NULL AUTO_INCREMENT,
  20. `city` varchar(255) NOT NULL,
  21. `state_id` int(11) NOT NULL COMMENT 'state_id from the states table',
  22. PRIMARY KEY(`city_id`)
  23. )
Download the complete source code for sample data for the above tables.

Database Connection (db.php)

  1. <?php
  2. define('HOSTNAME','localhost');
  3. define('DB_USERNAME','DATABASE_USERNAME');
  4. define('DB_PASSWORD','DATABASE_PASSWORD');
  5. define('DB_NAME', 'DATABASE_NAME');
  6. $con = mysqli_connect(HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME) or die ("error");
  7. // Check connection
  8. if(mysqli_connect_errno($con)) echo "Failed to connect MySQL: " .mysqli_connect_error();
  9. ?>
The above PHP code is responsible for establishing the database connection with MySQL. Change the database name, username, and password with your credentials.

HTML – Populate Multiple Dropdown Lists (index.php)

  1. <?php include('db.php'); ?>
  2. <div class="container-fluid">
  3. <div class="row">
  4. <div class="col-xs-12 col-md-sm-6 col-md-3">
  5. <label>Continent :</label>
  6. <select name="continent" class="form-control" id="continent">
  7. <option value=''>------- Select --------</option>
  8. <?php
  9. $sql = "select * from `continents`";
  10. $res = mysqli_query($con, $sql);
  11. if(mysqli_num_rows($res) > 0) {
  12. while($row = mysqli_fetch_object($res)) {
  13. echo "<option value='".$row->continent_id."'>".$row->continent."</option>";
  14. }
  15. }
  16. ?>
  17. </select>
  18. </div>
  19. <div class="col-xs-12 col-md-sm-6 col-md-3">
  20. <label>Country :</label>
  21. <select name="country" class="form-control" id="country" disabled="disabled"><option>------- Select --------</option></select>
  22. </div>
  23. <div class="col-xs-12 col-md-sm-6 col-md-3">
  24. <label>State / Province / County :</label>
  25. <select name="state" class="form-control" id="state" disabled="disabled"><option>------- Select --------</option></select>
  26. </div>
  27. <div class="col-xs-12 col-md-sm-6 col-md-3">
  28. <label>City / Popular Place :</label>
  29. <select name="city" class="form-control" id="city" disabled="disabled"><option>------- Select --------</option></select>
  30. </div>
  31. </div>
  32. </div>
Only for the continent dropdown list the list of continents will be generated through the PHP code on page load or page refresh.

Ajax – Populate Multiple Dropdown Lists using Ajax (mtb.js)

  1. $(document).ready(function() {
  2. //Change in continent dropdown list will trigger this function and
  3. //generate dropdown options for county dropdown
  4. $(document).on('change','#continent', function() {
  5. var continent_id = $(this).val();
  6. if(continent_id != "") {
  7. $.ajax({
  8. url:"get_data.php",
  9. type:'POST',
  10. data:{continent_id:continent_id},
  11. success:function(response) {
  12. //var resp = $.trim(response);
  13. if(response != '') {
  14. $("#country").removeAttr('disabled','disabled').html(response);
  15. $("#state, #city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  16. } else {
  17. $("#country, #state, #city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  18. }
  19. }
  20. });
  21. } else {
  22. $("#country, #state, #city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  23. }
  24. });
  25. //Change in coutry dropdown list will trigger this function and
  26. //generate dropdown options for state dropdown
  27. $(document).on('change','#country', function() {
  28. var country_id = $(this).val();
  29. if(country_id != "") {
  30. $.ajax({
  31. url:"get_data.php",
  32. type:'POST',
  33. data:{country_id:country_id},
  34. success:function(response) {
  35. //var resp = $.trim(response);
  36. if(response != '') {
  37. $("#state").removeAttr('disabled','disabled').html(response);
  38. $("#city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  39. }
  40. else $("#state, #city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  41. }
  42. });
  43. } else {
  44. $("#state, #city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  45. }
  46. });
  47. //Change in state dropdown list will trigger this function and
  48. //generate dropdown options for city dropdown
  49. $(document).on('change','#state', function() {
  50. var state_id = $(this).val();
  51. if(state_id != "") {
  52. $.ajax({
  53. url:"get_data.php",
  54. type:'POST',
  55. data:{state_id:state_id},
  56. success:function(response) {
  57. if(response != '') $("#city").removeAttr('disabled','disabled').html(response);
  58. else $("#city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  59. }
  60. });
  61. } else {
  62. $("#city").attr('disabled','disabled').html("<option value=''>------- Select --------</option>");
  63. }
  64. });
  65. });
The above jQuery and Ajax code are responsible for generating or populating the multiple dropdown lists.

PHP – Fetch Data From MySQL Database (get_data.php)

  1. <?php include('db.php');?>
  2. <?php
  3. if (isset($_POST['continent_id'])) {
  4. $qry = "select * from countries_new where continent_id=".mysqli_real_escape_string($con,$_POST['continent_id'])." order by country";
  5. $res = mysqli_query($con, $qry);
  6. if(mysqli_num_rows($res) > 0) {
  7. echo '<option value="">------- Select -------</option>';
  8. while($row = mysqli_fetch_object($res)) {
  9. echo '<option value="'.$row->country_id.'">'.$row->country.'</option>';
  10. }
  11. } else {
  12. echo '<option value="">No Record</option>';
  13. }
  14. } else if(isset($_POST['country_id'])) {
  15. $qry = "select * from states_new where country_id=".mysqli_real_escape_string($con,$_POST['country_id'])." order by state";
  16. $res = mysqli_query($con, $qry);
  17. if(mysqli_num_rows($res) > 0) {
  18. echo '<option value="">------- Select -------</option>';
  19. while($row = mysqli_fetch_object($res)) {
  20. echo '<option value="'.$row->state_id.'">'.$row->state.'</option>';
  21. }
  22. } else {
  23. echo '<option value="">No Record</option>';
  24. }
  25. } else if(isset($_POST['state_id'])) {
  26. $qry = "select * from cities where state_id=".mysqli_real_escape_string($con,$_POST['state_id'])." order by city";
  27. $res = mysqli_query($con, $qry);
  28. if(mysqli_num_rows($res) > 0) {
  29. echo '<option value="">------- Select -------</option>';
  30. while($row = mysqli_fetch_object($res)) {
  31. echo '<option value="'.$row->city_id.'">'.$row->city.'</option>';
  32. }
  33. } else {
  34. echo '<option value="">No Record</option>';
  35. }
  36. }
  37. ?>
Based on the continent_idcoutry_id and state_id, data will fetch from the database respectively.


Ref http://www.mitrajit.com/populate-multiple-dropdown-lists-using-ajax-jquery-php-mysql/


bit project class bit project guidelines bit project proposal sample bit project sample bit project schedule
bit project qub bit project management bitcoin projected value bitbucket projects bit project vle bit project help bit project topics 16 bit audiophile project 8 bit alu project report 2 bit alu project 4 bit adder project 4 bit alu project micro bit accelerometer project 8 bit project riz and yoshi bit project beograd
bitbucket project key bitbucket project management micro bit breadboard project micro bit bluetooth project bitcoin project ppt bitcoin project report 4 bit calculator project 8 bit computer project
4 bit counter project bitbucket create project clone project from bitbucket how to delete project from bitbucket 8 bit darling project reaper project bit depth 32 bit microsoft project download http //bit.ly/project dishari after effects project bit depth drake bit project pat but darling project 32 bit ms project free download fixed bid project micro bit project game bit.ly/project learn hse bit heroes project
little bit project ideas bit final year project ideas 2 bit project jump up and down micro bit project kits
bit.ly/project key bbc micro bit project kit bit.ly/project scorpio server 7 micro bit led project bit-tech.net project logs bit project meaning bit mesra project project bitmap 2-bit project - make u dance 64 bit microsoft project 32 bit ms project 2010 download 64 bit project navigator micro bit new project bitbucket change project name bitbucket create new project bit of project management micro bit project pdf drill bit project pdf how to push project to bitbucket bit ranchi project position project bit rate
2-bit project - rock the house yaule micro bit radio project micro bit robot project the bit rot project obs project bitrate project bit stuffing bit.ly/project scorpion micro bit save project bit open source project
router bit storage project 64 bit project visual studio bit town project upload project to bitbucket ucsc bit project universal bit project v bit cnc projects micro bit watch project w bot project 64 bit xcode project  logic pro x project bit depth bit final year project project zomboid bit 11 bit project 8 32 bit project 2013 2 bit project 2 bit calculator project 2 bit magnitude comparator project project 32 bit download project 32 bit error 4 bit microprocessor projects project 64 bit project 64 bit 2016 8-bit projection mapping 8 bit microcontroller projects 8 bit cpu projects


IELTS exam sample learning materials To achieve a 7.5 or above

To achieve a 7.5 or above in the IELTS exam, you'll need to focus on the four main sections: Listening, Reading, Writing, and Speaking. ...