Skip to main content

PHP - Populate Select Tag Base On Category

In this tutorial we will create a Populate Select Tag Base On Category using PHP. This code will display a specific selection options from MySQLi when user select a category from the drop down list. The code use MySQLi SELECT query to display a data in the MySQLi row table by adding a value in "category" WHERE clause. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_category, after that click Import then locate the database file inside the folder of the application then click ok.

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=mysqli_connect("localhost", "root", "", "db_category");
  3. if(!$conn){
  4. die("Error: Failed to connecto 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 charse="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="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="">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 - Populate Select Tag Base On Category</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add brand</button>
  18. <br /><br />
  19. <form class="form-inline" method="POST" action="">
  20. <label>Select Category:</label>
  21. <select class="form-control" name="category">
  22. <option value="Suzuki">Suzuki</option>
  23. <option value="Honda">Honda</option>
  24. <option value="Kawasaki">Kawasaki</option>
  25. </select>
  26. <button class="btn btn-warning" name="populate">Filter</button>
  27. <br /><br />
  28. <h4>Selected Brand: <span class="text-primary"><?php echo $_POST['category']?></span></h4>
  29. <select class="form-control">
  30. <?php include'populate.php'?>
  31. </select>
  32. </form>
  33. </div>
  34. <div class="modal fade" id="form_modal" aria-hidden="true">
  35. <div class="modal-dialog">
  36. <div class="modal-content">
  37. <form method="POST" action="save.php">
  38. <div class="modal-header">
  39. <h3 class="modal-title">Add Brand</h3>
  40. </div>
  41. <div class="modal-body">
  42. <div class="col-md-2"></div>
  43. <div class="col-md-8">
  44. <div class="form-group">
  45. <label>Brand</label>
  46. <select name="brand" class="form-control" required="required">
  47. <option value="">Select an option</option>
  48. <option value="Suzuki">Suzuki</option>
  49. <option value="Honda">Honda</option>
  50. <option value="Kawasaki">Kawasaki</option>
  51. </select>
  52. </div>
  53. <div class="form-group">
  54. <label>Name</label>
  55. <input type="text" class="form-control" name="name" required="required"/>
  56. </div>
  57. </div>
  58. </div>
  59. <br style="clear:both;"/>
  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 class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  63. </div>
  64. </form>
  65. </div>
  66. </div>
  67. </div>
  68. <script src="js/jquery-3.2.1.min.js"></script>
  69. <script src="js/bootstrap.js"></script>
  70. </body>
  71. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the form inputs to the database server. To make this just copy and write these block of codes below inside the text editor, then save it as save.php

  1. <?php
  2. require_once'conn.php';
  4. if(ISSET($_POST['save'])){
  5. $brand=$_POST['brand'];
  6. $name=$_POST['name'];
  7. }
  8. mysqli_query($conn, "INSERT INTO `motors` VALUES('', '$brand', '$name')") or die(mysqli_error());
  9. header("location: index.php");
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will display a specific select options base on category when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as populate.php

  1. <?php
  2. require_once'conn.php';
  4. if(ISSET($_POST['populate'])){
  5. $category=$_POST['category'];
  6. $query=mysqli_query($conn, "SELECT * FROM `motors` WHERE `category`='$category'") or die(mysqli_error());
  7. while($fetch=mysqli_fetch_array($query)){
  8. echo"<option>".$fetch['name']."</option>";
  9. }
  10. }else{
  11. echo"<option>No Data</option>";
  12. }
  13. ?>

There you have it we successfully created Populate Select Tag Base On Category using PHP. 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!

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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.