Loading Drop Down Selection Using PHP
Submitted by alpha_luna on Tuesday, May 10, 2016 - 13:43.
If you are looking for on how to create Loading Drop Down Selection Using PHP then you are at the right place. This program works when the user selects from the drop-down list then the data from MySQL Table will display in the corresponding id. So, let's start with:
For the ProductsTable
Codes for the Drop Down List
Codes for the user Select in the Drop Down List
After the user select in the Drop Down List.
Thank you for reading this article. So, this is it, or you can download the full source code below by clicking the "Download Code" button below.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.
Creating our Table
We are going to make our database. To create a database:- Open the PHPMyAdmin.
- Create a database and name it as "dropDown_test".
- After creating a database name, then we are going to create our table. And name it as "categories" and "products".
- Kindly copy the code below.
- --
- -- Table structure for table `categories`
- --
- CREATE TABLE `categories` (
- `cat_id` INT(11) NOT NULL,
- `cat_name` VARCHAR(20) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- --
- -- Table structure for table `products`
- --
- CREATE TABLE `products` (
- `product_id` INT(11) NOT NULL,
- `product_name` VARCHAR(50) NOT NULL,
- `cat_id` INT(11) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Database Connection Using PDO
- <?php
- $dbcon = new PDO("mysql:host=localhost;dbname=dropDown_test",'root','');
- ?>
- <select id="products_Motor">
- <option value="showAll" selected="selected">Show All Brands</option>
- <?php
- require_once 'config.php';
- $result = $dbcon->prepare('SELECT * FROM categories');
- $result->execute();
- while($row=$result->fetch(PDO::FETCH_ASSOC))
- {
- ?>
- <option value="<?php echo $cat_id; ?>"><?php echo $cat_name; ?></option>
- <?php
- }
- ?>
- </select>
- <?php
- include('config.php');
- $action = $_REQUEST['action'];
- if($action=="showAll"){
- $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products ORDER BY product_name');
- $stmt->execute();
- }else{
- $stmt=$dbcon->prepare('SELECT product_id, product_name FROM products WHERE cat_id=:cid ORDER BY product_name');
- }
- ?>
- <div class="row">
- <?php
- if($stmt->rowCount() > 0){
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- ?>
- <div class="col-xs-3">
- <div style="border-radius:10px; border:blue solid 1px; background:azure; color:blue; padding:22px;"><?php echo $product_name; ?></div>
- <br />
- </div>
- <?php
- }
- }else{
- ?>
- <div class="col-xs-3">
- <div style="border-radius:3px; border:#cdcdcd solid 1px; padding:22px;"><?php echo $product_name; ?></div>
- <br />
- </div>
- <?php
- }
- ?>
- </div>
Output:
The user select in the Drop Down List.

Comments
Add new comment
- Add new comment
- 1082 views