PHP - Populate Select Tag Base On Category

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

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 https://www.apachefriends.org/index.html.

And, 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_category, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Populate Select Tag Base On Category</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add brand</button>
  19. <br /><br />
  20. <form class="form-inline" method="POST" action="">
  21. <label>Select Category:</label>
  22. <select class="form-control" name="category">
  23. <option value="Suzuki">Suzuki</option>
  24. <option value="Honda">Honda</option>
  25. <option value="Kawasaki">Kawasaki</option>
  26. </select>
  27. <button class="btn btn-warning" name="populate">Filter</button>
  28. <br /><br />
  29. <h4>Selected Brand: <span class="text-primary"><?php echo $_POST['category']?></span></h4>
  30. <select class="form-control">
  31. <?php include'populate.php'?>
  32. </select>
  33. </form>
  34. </div>
  35. <div class="modal fade" id="form_modal" aria-hidden="true">
  36. <div class="modal-dialog">
  37. <div class="modal-content">
  38. <form method="POST" action="save.php">
  39. <div class="modal-header">
  40. <h3 class="modal-title">Add Brand</h3>
  41. </div>
  42. <div class="modal-body">
  43. <div class="col-md-2"></div>
  44. <div class="col-md-8">
  45. <div class="form-group">
  46. <label>Brand</label>
  47. <select name="brand" class="form-control" required="required">
  48. <option value="">Select an option</option>
  49. <option value="Suzuki">Suzuki</option>
  50. <option value="Honda">Honda</option>
  51. <option value="Kawasaki">Kawasaki</option>
  52. </select>
  53. </div>
  54. <div class="form-group">
  55. <label>Name</label>
  56. <input type="text" class="form-control" name="name" required="required"/>
  57. </div>
  58. </div>
  59. </div>
  60. <br style="clear:both;"/>
  61. <div class="modal-footer">
  62. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  63. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  64. </div>
  65. </form>
  66. </div>
  67. </div>
  68. </div>
  69. <script src="js/jquery-3.2.1.min.js"></script>
  70. <script src="js/bootstrap.js"></script>
  71. </body>
  72. </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';
  3.  
  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';
  3.  
  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • 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.