Introduction

A dropdown list is a common form element used to allow users to select one option from a predefined list. In many web applications, the dropdown values need to be retrieved dynamically from a database instead of being manually added to the HTML code.

In this post, we will explain how to create a dynamic dropdown list using HTML, PHP, and MySQL. The country names will be stored in a MySQL table, and PHP will retrieve the records from the database and display them as dropdown options.

Prerequisites

Before implementing the dynamic dropdown list, make sure you have the following:

  • Basic knowledge of HTML and PHP.
  • A working PHP environment such as XAMPP, WAMP, LAMP, or a similar web server.
  • MySQL or MariaDB installed and running.
  • A MySQL database created for the application.
  • Basic knowledge of MySQL queries.
  • A PHP file where the dropdown list code can be added.

IMPLEMENTATION

Dynamic dropdown list using PHP

Step 1: Create a new file, dropdown.php

Step 2:  Create a MySQL connection code inside dropdown.php

$mysqlserver="localhost";
$mysqlusername="root";
$mysqlpassword="";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'test';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

Step3: Create a MySQL table ls_countries

CREATE TABLE `ls_countries` (  `country_id` int(11) NOT NULL AUTO_INCREMENT,  `sortname` varchar(3) NOT NULL,  `name` varchar(150) NOT NULL,  `phonecode` int(11) NOT NULL,  `status` int(11) NOT NULL COMMENT '1->active,0->deactive',  PRIMARY KEY (`country_id`)) ENGINE=InnoDB AUTO_INCREMENT=247 DEFAULT CHARSET=utf8

Step 4: Create a drop-down list dynamically using the following code

<select>
  <option>Select Country</option>
  <?php
   $cdquery="SELECT * FROM ls_countries";
            $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());
            
            while ($cdrow=mysql_fetch_array($cdresult)) {
            $cdTitle=$cdrow["name"];
                echo "<option>$cdTitle </option>";
            
            }
  ?>
</select>

We are  following the above steps and getting code like this

<?php 
$mysqlserver="localhost";
$mysqlusername="root";
$mysqlpassword="";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'linus';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
?>
<!DOCTYPE html>
<html>
<head>
  <title>Drop Down Using Php</title>
</head>
<body>
<select>
  <option>Select Country</option>
  <?php
   $cdquery="SELECT * FROM ls_countries";
            $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());
            
            while ($cdrow=mysql_fetch_array($cdresult)) {
            $cdTitle=$cdrow["name"];
                echo "<option>$cdTitle </option>";
            
            }
  ?>
</select>
</body>
</html>

Conclusion

In this tutorial, we learned how to create a dynamic country dropdown list using PHP and MySQL. Instead of manually adding each country to the HTML <select> element, PHP retrieves the country names from the MySQL database and generates the dropdown options dynamically.

This approach is useful when dropdown values are stored in a database and may change over time. The same concept can also be extended to create dynamic state, city, category, department, and other dependent dropdown lists.

For modern PHP applications, it is recommended to replace the deprecated mysql_* functions with MySQLi or PDO and use prepared statements for better security and compatibility.

Leave a Reply