How to Load Data using Drop Down Selection in PHP

If you are looking for on how to Load Data using Drop Down Selection in PHP then you are at the right place. This simple program runs when the users select from the drop-down list then the data from MySQL Table will display in the corresponding id. So, let's start with:

Creating Simple Markup

This simple markup contains select element and div tag where the user can select in the drop-down list to load the desired data.
  1. <div class="page-header">
  2.         <h3>
  3.                 <select id="products_Motor" style="color:blue;">
  4.                 <option value="showAll" selected="selected">Show All Brands</option>
  5.                 <?php
  6.                 require_once 'config.php';
  7.                
  8.                 $result = $dbcon->prepare('SELECT * FROM categories');
  9.                 $result->execute();
  10.                
  11.                 while($row=$result->fetch(PDO::FETCH_ASSOC))
  12.                 {
  13.                 extract($row);
  14.                 ?>
  15.                 <option value="<?php echo $cat_id; ?>"><?php echo $cat_name; ?></option>
  16.                 <?php
  17.                 }
  18.                 ?>
  19.                 </select>
  20.         </h3>
  21. </div>
  22.  
  23. <h3 style="color:blue;">Load Data using Drop Down Selection in PHP</h3>
  24. <hr />
  25.    
  26. <div class="" id="display"></div>
  27.    
  28. </div>

JavaScript Source Code

This script codes that we are going to use has a function to get all the records from our table by using our selector. Kindly copy and paste to your HEAD tag of your page.
  1. <script type="text/javascript">
  2. $(document).ready(function()
  3. {              
  4.         // function to get all records from table
  5.         function getAll(){
  6.                
  7.                 $.ajax
  8.                 ({
  9.                         url: 'products_Motor.php',
  10.                         data: 'action=showAll',
  11.                         cache: false,
  12.                         success: function(r)
  13.                         {
  14.                                 $("#display").html(r);
  15.                         }
  16.                 });                    
  17.         }
  18.        
  19.         getAll();
  20.         // function to get all records from table
  21.        
  22.        
  23.         // code to get all records from table via select box
  24.         $("#products_Motor").change(function()
  25.         {                              
  26.                 var id = $(this).find(":selected").val();
  27.  
  28.                 var dataString = 'action='+ id;
  29.                                
  30.                 $.ajax
  31.                 ({
  32.                         url: 'products_Motor.php',
  33.                         data: dataString,
  34.                         cache: false,
  35.                         success: function(r)
  36.                         {
  37.                                 $("#display").html(r);
  38.                         }
  39.                 });
  40.         })
  41.         // code to get all records from table via select box
  42. });
  43. </script>

PHP Source Code

This PHP code we are going to use to load the data from the table using a selector to display on our page. Save it as "products_Motor.php".
  1. <?php
  2. include('config.php');
  3.  
  4. $action = $_REQUEST['action'];
  5.  
  6. if($action=="showAll"){
  7.        
  8.         $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products ORDER BY product_name');
  9.         $stmt->execute();
  10.        
  11. }else{
  12.        
  13.         $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products WHERE cat_id=:cid ORDER BY product_name');
  14.         $stmt->execute(array(':cid'=>$action));
  15. }
  16. ?>
  17.  
  18. <div class="row">
  19. <?php
  20. if($stmt->rowCount() > 0){
  21. while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  22. {
  23. extract($row);
  24.  
  25. ?>
  26. <div class="col-xs-3">
  27.         <div style="border-radius:10px; border:blue solid 1px; background:azure; color:blue; padding:22px;"><?php echo $product_name; ?></div>
  28.         <br />
  29. </div>
  30. <?php          
  31. }
  32.  
  33. }else{
  34. ?>
  35. <div class="col-xs-3">
  36.         <div style="border-radius:3px; border:#cdcdcd solid 1px; padding:22px;"><?php echo $product_name; ?></div>
  37.         <br />
  38. </div>
  39.  
  40. <?php          
  41. }
  42. ?>
  43. </div>

Output


Result For the full source code, kindly click the "Download Code" button below. Hope that this simple tutorial that I created may help you to your future projects. Also, for the beginners who want to learn basic of coding in PHP/MySQL and JavaScript. If you are interested in programming, we have an example of programs that may help you even just in small ways. So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment