Dependent Select Option With MySQLi Using jQuery/PHP

Language

The Dependent select option was build using jQuery / PHP. This simple source code will show you how to create a dependent select option driven by MySQLi database server. The select option is automatically generate whenever the user choose from the first selection. It retrieve the data within the database to display the result based on the selected value. This is the sample code
  1. <!DOCTYPE html>
  2. <?php
  3. $conn = new mysqli('localhost', 'root', '', 'db_animal') or die(mysqli_error());
  4. ?>
  5. <html lang = "eng">
  6. <head>
  7. <meta charset = "UTF-8" />
  8. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  9. </head>
  10. <body>
  11. <nav class = "navbar navbar-default">
  12. <div class = "container-fluid">
  13. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  14. </div>
  15. </nav>
  16. <div class = "container-fluid">
  17. <div class = "row">
  18. <div class = "col-md-3"></div>
  19. <div class = "col-md-6 well">
  20. <h4 class = "text-primary">Dependent Select Option With MySQLi Using PHP / jQuery</h4>
  21. <hr style = "border-top: 1px dotted #8c8b8b;"/>
  22. <form class = "form-inline">
  23. <div class = "form-group">
  24. <label>Animal Group:</label>
  25. <select id = "group" class = "form-control" name = "animal-groupl" required = "required">
  26. <option value = "">Select a group</option>
  27. <?php
  28. $g_animal = $conn->prepare("SELECT * FROM `animal` GROUP BY `animal_group`");
  29. if($g_animal->execute()){
  30. $g_result = $g_animal->get_result();
  31. }
  32. while($f_ganimal = $g_result->fetch_array()){
  33. ?>
  34. <option value = "<?php echo $f_ganimal['animal_group']?>"><?php echo $f_ganimal['animal_group']?></option>
  35. <?php
  36. }
  37. $conn->close();
  38. ?>
  39. </select>
  40. </div>
  41. <div class = "form-group">
  42. <label>Animal:</label>
  43. <select id = "animal" name = "animal" class = "form-control" disabled = "disabled" required = "required">
  44. <option value = "">Select an animal</option>
  45. </select>
  46. </div>
  47. </form>
  48. </div>
  49. </div>
  50. </div>
  51. </body>
  52. <script src = "js/jquery-3.1.1.js"></script>
  53. <script type = "text/javascript">
  54. $(document).ready(function(){
  55. $('#group').on('change', function(){
  56. if($('#group').val() == ""){
  57. $('#animal').empty();
  58. $('<option value = "">Select an animal</option>').appendTo('#animal');
  59. $('#animal').attr('disabled', 'disabled');
  60. }else{
  61. $('#animal').removeAttr('disabled', 'disabled');
  62. $('#animal').load('animal_get.php?animal_group=' + $('#group').val());
  63. }
  64. });
  65. });
  66. </script>
  67. </html>
I hope the source code can help you to your projects and future projects. For more update and tutorial, just kindly visit this site. Enjoy coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment