• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Dependent Dropdown List with PHP, jQuery and Ajax.

II MR V1K1NG II

Hardware Engineer
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
59
Likes
112
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
There are times in a web application where you want to populate a dropdown list based on the value of another drop down list. Scenarios like this can be populating a Country’s State dropdown based on the value of the Country selected, populating product sub-categories based on the parent category. In this example, we will be creating a dropdown list for category/subcategory for a product in an eCommerce website.
Create a database called dependent_list.

We will Create 2 tables: categories and subcategories with the following queries:
  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `categories`

    (
  2. `id`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `category_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  4. PRIMARY

    KEY

    (

    `id`

    )
  5. )

    ENGINE=

    InnoDB;

  6. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `subcategories`

    (
  7. `id`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  8. `categoryID`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  9. `subcategory_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  10. PRIMARY

    KEY

    (

    `id`

    )
  11. )

    ENGINE=

    InnoDB;

Some data has been inserted into both tables as shown in database. categoryID is a foreign key in subcategories table i.e 1 category has multiple subcategories.

Create a project folder called ‘dependent_list’ in your site root folder. Create a config.php file to store the database connection and add the following code:
  1. <?php

  2. mysql_connect

    (

    'localhost'

    ,

    'root'

    ,

    ''

    )

    ;
  3. mysql_select_db

    (

    'dependent_list'

    )

    ;

  4. ?>

Next Create an index.php file in the project folder and add the following code:
  1. <?php
  2. include

    (

    'config.php'

    )

    ;
  3. $query_parent

    =

    mysql_query

    (

    "SELECT * FROM categories"

    )

    or die

    (

    "Query failed: "

    .

    mysql_error

    (

    )

    )

    ;
  4. ?>

  1. <!doctype html>
  2. <html

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

    Dependent DropDown List</

    title

    >
  6. <script

    type

    =

    "text/javascript"

    src

    =

    "js/jquery.js"

    ></

    script

    >
  7. <script

    type

    =

    "text/javascript"

    >
  8. $(document).ready(function() {

  9. $("#parent_cat").change(function() {
  10. $(this).after('<div

    id

    =

    "loader"

    ><img

    src

    =

    "img/loading.gif"

    alt

    =

    "loading subcategory"

    /

    ></

    div

    >

    ');
  11. $.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
  12. $("#sub_cat").html(data);
  13. $('#loader').slideUp(200, function() {
  14. $(this).remove();
  15. });
  16. });
  17. });

  18. });
  19. </

    script

    >
  20. </

    head

    >

  21. <body

    >
  22. <form

    method

    =

    "get"

    >
  23. <label

    for

    =

    "category"

    >

    Parent Category</

    label

    >
  24. <select

    name

    =

    "parent_cat"

    id

    =

    "parent_cat"

    >
  25. <?php while(

    $row =

    mysql_fetch_array(

    $query_parent)

    )

    : ?>
  26. <option

    value

    =

    "<?php echo $row['id']; ?>

    "><?php echo $row[

    'category_name'

    ]

    ; ?></

    option

    >
  27. <?php endwhile; ?>
  28. </

    select

    >
  29. <br

    /

    ><br

    /

    >

  30. <label

    >

    Sub Category</

    label

    >
  31. <select

    name

    =

    "sub_cat"

    id

    =

    "sub_cat"

    ></

    select

    >
  32. </

    form

    >
  33. </

    body

    >
  34. </

    html

    >

On line 3, we queried our categories table to get all categories. We then populate the parent_cat dropdownlist with the categories on lines 33-37. Whenever the dropdown value for category is changed, a jquery changed event is triggered for the category dropdown list on line 10. On lines 10-18,it sends the id value of the selected category through jquery Ajax to a php script called loadsubcat.php which then queries the subcategories table for subcategories that belongs to the parent category id value using $_GET[] super global . The values returned is now appended to the sub_cat dropdown list. We also added an animated loading gif for user experience, it is displayed when the a value is selected for the parent category and removed using a jquery “slideUp” method after the subcategory has been populated.

Lastly, create a loadsubcat.php file in the project folder and add the following code.
  1. <?php
  2. include

    (

    'config.php'

    )

    ;

  3. $parent_cat

    =

    $_GET

    [

    'parent_cat'

    ]

    ;

  4. $query

    =

    mysql_query

    (

    "SELECT * FROM subcategories WHERE categoryID = {$parent_cat}

    "

    )

    ;
  5. while

    (

    $row

    =

    mysql_fetch_array

    (

    $query

    )

    )

    {
  6. echo

    "<option value='$row[id]

    '>$row[subcategory_name]

    </option>"

    ;
  7. }

  8. ?>

Now go to http://localhost/dependent_list and you will see how the dependent dropdownlist works


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top