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

PHP - Simple Dynamic Dependent Select Box Using jQuery

Toiy

Script Execution Specialist
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
160
Likes
138
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will try to create a Simple Dynamic Dependent Select Box Using jQuery. jQuery is a cross-platform javaScript library design to simplified the modern HTML client side scripting. jQuery syntax was made to make it easier to navigate a document, like select DOM elements, develop Ajax applications and etc. So let's start coding...

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_food, after that click Import then locate the database file inside the folder of the application then click ok.
2018-06-18_15_24_54-localhost_127.0.0.1_db_food_phpmyadmin_4.7.0.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_food'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Can't connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" tyoe="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Dynamic Dependent Select Box</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal">Add Data</button>
  18. <br /><br />
  19. <div class = "form-inline">
  20. <label>Food Category:</label>
  21. <select id = "category" class = "form-control" required = "required">
  22. <option value = "">Select a Category</option>
  23. <?php
  24. require

    'conn.php'

    ;

  25. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `food` GROUP BY `food_category`"

    )

    ;
  26. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {

  27. ?>
  28. <option value="<?php

    echo

    $fetch

    [

    'food_category'

    ]

    ?>

    "><?php

    echo

    $fetch

    [

    'food_category'

    ]

    ?>

    </option>
  29. <?php
  30. }
  31. ?>
  32. </select>
  33. <label>Food:</label>
  34. <select id = "food" class = "form-control" disabled = "disabled" required = "required">
  35. <option value = "">Select a food</option>
  36. </select>
  37. </div>
  38. </div>
  39. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  40. <div class="modal-dialog" role="document">
  41. <form action="save_query.php" method="POST">
  42. <div class="modal-content">
  43. <div class="modal-body">
  44. <div class="col-md-2"></div>
  45. <div class="col-md-8">
  46. <div class="form-group">
  47. <label>Food Category</label>
  48. <input class="form-control" type="text" name="food_category">
  49. </div>
  50. <div class="form-group">
  51. <label>Food</label>
  52. <input class="form-control" type="text" name="food" />
  53. </div>
  54. <div class="form-group">
  55. <button class="btn btn-primary form-control" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  56. </div>
  57. </div>
  58. </div>
  59. <div style="clear:both;"></div>
  60. <div class="modal-footer">
  61. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  62. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  63. </div>
  64. </div>
  65. </form>
  66. </div>
  67. </div>
  68. </body>
  69. <script src="js/jquery-3.2.1.min.js"></script>
  70. <script src="js/bootstrap.js"></script>
  71. <script src="js/script.js"></script>
  72. </html>

Creating PHP Queries

This code contains the php query of the application. This code can store your data inputs to the database server, and then will return a data from the database to html jQuery request method. To do that just copy and write this block of codes inside the text editor, then save it as shown below.
save_query.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. if

    (

    !

    empty

    (

    $_POST

    [

    'food_category'

    ]

    )

    &&

    !

    empty

    (

    $_POST

    [

    'food'

    ]

    )

    )

    {
  5. $food_category

    =

    addslashes

    (

    $_POST

    [

    'food_category'

    ]

    )

    ;
  6. $food

    =

    addslashes

    (

    $_POST

    [

    'food'

    ]

    )

    ;


  7. $conn

    ->

    query

    (

    "INSERT INTO `food` VALUES('', '$food_category

    ', '$food

    ')"

    )

    ;
  8. echo

    "<script>alert('Data Inserted!')</script>"

    ;
  9. echo

    "<script>window.location = 'index.php'</script>"

    ;
  10. }

    else

    {
  11. echo

    "<script>alert('Please complete the required field!')</script>"

    ;
  12. echo

    "<script>window.location = 'index.php'</script>"

    ;
  13. }
  14. }
  15. ?>

food_get.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `food` WHERE `food_category` = '$_REQUEST[food_category]

    '"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  4. echo

    '<option value = "">Select a food</option>'

    ;

  5. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  6. echo

    '<option value = "'

    .

    $fetch

    [

    'food'

    ]

    .

    '">'

    .

    $fetch

    [

    'food'

    ]

    .

    '</option>'

    ;
  7. }
  8. ?>

Creating The jQuery Script

This is where the code that uses jquery script been used. This code will retrieve the target data from the database to be display in the select option. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '#category'

    )

    .on

    (

    'change'

    ,

    function

    (

    )

    {
  3. if

    (

    $(

    '#category'

    )

    .val

    (

    )

    ==

    ""

    )

    {
  4. $(

    '#food'

    )

    .empty

    (

    )

    ;
  5. $(

    '<option value = "">Select a food</option>'

    )

    .appendTo

    (

    '#food'

    )

    ;
  6. $(

    '#food'

    )

    .attr

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  7. }

    else

    {
  8. $(

    '#food'

    )

    .removeAttr

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  9. $(

    '#food'

    )

    .load

    (

    'food_get.php?food_category='

    +

    $(

    '#category'

    )

    .val

    (

    )

    )

    ;
  10. }
  11. }

    )

    ;
  12. }

    )

    ;

There you have it we successfully created a Simple Dynamic Dependent Select Box Using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


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

452,158

323,328

323,337

Top