Dependent Select Option With MySQLi Using jQuery/PHP

Submitted by: 
Language: 
Visitors have accessed this post 2454 times.

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 = "<a href="https://www.sourcecodester.com">Sourcecodester</a>
  14. " rel="nofollow">https://www.sourcecodester.com">Sourcecodester</a>
  15. </a> </div>
  16. </nav>
  17. <div class = "container-fluid">
  18. <div class = "row">
  19. <div class = "col-md-3"></div>
  20. <div class = "col-md-6 well">
  21. <h4 class = "text-primary">Dependent Select Option With MySQLi Using PHP / jQuery</h4>
  22. <hr style = "border-top: 1px dotted #8c8b8b;"/>
  23. <form class = "form-inline">
  24. <div class = "form-group">
  25. <label>Animal Group:</label>
  26. <select id = "group" class = "form-control" name = "animal-groupl" required = "required">
  27. <option value = "">Select a group</option>
  28. <?php
  29. $g_animal = $conn->prepare("SELECT * FROM `animal` GROUP BY `animal_group`");
  30. if($g_animal->execute()){
  31. $g_result = $g_animal->get_result();
  32. }
  33. while($f_ganimal = $g_result->fetch_array()){
  34. ?>
  35. <option value = "<?php echo $f_ganimal['animal_group']?>"><?php echo $f_ganimal['animal_group']?></option>
  36. <?php
  37. }
  38. $conn->close();
  39. ?>
  40. </select>
  41. </div>
  42. <div class = "form-group">
  43. <label>Animal:</label>
  44. <select id = "animal" name = "animal" class = "form-control" disabled = "disabled" required = "required">
  45. <option value = "">Select an animal</option>
  46. </select>
  47. </div>
  48. </form>
  49. </div>
  50. </div>
  51. </div>
  52. </body>
  53. <script src = "js/jquery-3.1.1.js"></script>
  54. <script type = "text/javascript">
  55. $(document).ready(function(){
  56. $('#group').on('change', function(){
  57. if($('#group').val() == ""){
  58. $('#animal').empty();
  59. $('<option value = "">Select an animal</option>').appendTo('#animal');
  60. $('#animal').attr('disabled', 'disabled');
  61. }else{
  62. $('#animal').removeAttr('disabled', 'disabled');
  63. $('#animal').load('animal_get.php?animal_group=' + $('#group').val());
  64. }
  65. });
  66. });
  67. </script>
  68. </html>

style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-3664195971165750"
data-ad-slot="8323940646">

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!!


Tags: 

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.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.