PHP and MySQL Code

Start typing to search for PHP and MySQL Code Snippets and Articles Search

Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
Title : Dynamic Dropdown for Country and City List
Categories : PHP, Java Script, HTML and PHP Click here to Update Your Picture
Ehsan Haque
Date : May 17th 2006
Grade : 3 of 5 (graded 31 times)
Viewed : 92543
File :
Images : No Images for this code example.
Search : More code by Ehsan Haque
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 

This example shows how to create a Dynamic List for Country and City. Select a country from dropdown menu and it reloads the page to show the cities in another dropdown.

This example shows how to create a Dynamic List for Country and City
-- It shows a list of country
-- When a country is selected, it generates a drop down list with its cities
-- In the php functions, getCountryList() and getCityList()
-- for example purpose the array of country and city is hardcoded
-- the array of list can be replaced by resultset from a database

@Author: Ehsan Haque

<SCRIPT language=JavaScript>
function reload(form)
// Setting the variable with the value of selected country's ID
var val=populate.countryList.options[populate.countryList.options.selectedIndex].value;

// Sending the country id in the query string to retrieve the city list
self.location='populate.php?countryId=' + val ;


- Function to return the Country list as an array
- The array can be generated from a database resultset
function getCountryList()
// Country List array
$countryList    = array (
'1' => 'Bangladesh',
'2' => 'USA',
'3' => 'UK'

- Function to return the City list as an array
- Country ID is used to generate the city list
function getCityList($countryId)
// City list array
  // First key of the array is the Country ID, which holds an array of City list
$cityList       = array (
'1' => array ('Dhaka', 'Chittagong', 'What else'),
'3' => array ('London', 'Cannot Remember'),
'2' => array ('Washington', 'N.Y.', 'etc')

<form action="populate.php" name="populate">

// Retrieving the country list
$countryList  = getCountryList();

// Setting the variable if the country is selected for its city list
@$countryId  = $_GET['countryId'];

// Retrieving the city list if a country is selected
$cityList   = ($countryId) ? getCityList($countryId) : null;

if (!empty(
// Generating the country drop down menu
echo "<select onChange='reload(this.form)' name='countryList'>";
  foreach (
$countryList as $key => $value)
"<option value='$key'";
    if (
$countryId == $key)

if (!empty(
// Generating the city drop down menu if a country is selected
echo "<select name='cityList'>";
  foreach (
$cityList as $key => $value)
"<option value='$key'>$value</option>";


This PHP function creates dropdown select lists for time and date that you can change, outputs a 14 char MySQL timestamp in a text field
Categories : PHP, MySQL, Java Script, HTML and PHP
Newbie Notes #9 - Hyperlinking a post
Categories : PHP, Java Script, HTML and PHP, Beginner Guides
Function that allows a Javascript cookie to be set after HTML has been outputted to the page.
Categories : PHP, Java Script, Cookies, HTML and PHP
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
Validator 98 - a PHP-script to generate form-validation-code in JavaScript.
Categories : Complete Programs, Java Script, PHP, HTML and PHP
Tree Menu Dynamic (+Static) with Loading in Progress..
Categories : PHP, Java Script, HTML and PHP
OverEasy - PHP generated JavaScript to do mouseovers on your pages. Modify one file and one function does it all for you!
Categories : PHP, Java Script, HTML and PHP, MySQL
Remote Scripting: send form POST data to a script and insert the results into a page without refreshing the page.
Categories : PHP, AJAX, HTML and PHP, Java Script
PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions
Local-to-user date and time display regardless of time zone or where the website's server is located
Categories : PHP, Date Time, HTML and PHP, Java Script
Dynamic Calendar in PHP, Javascript and HTML.
Categories : PHP, Java Script, HTML and PHP, Calendar
Dynamic generation of textboxes, select items etc in a table for use with databases applications, matrimonials and for job sites
Categories : PHP, HTML and PHP, Java Script
Javascript/DHTML menus without all the extra javascipt.... server side browser detection + dynamically generated javascript (build menus on fly using php)
Categories : PHP, Java Script, Navigation
Paginating the mySQL data
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
 naveen chary wrote : 1856
thank you it works gr8...
 velora chan wrote : 1859
I was wondering what your code was in populate.php because when I try the code, the list of cities won't come up. It says object not found after I click on a country.

 Mainul Hasan wrote :1936
hi Ehsan Bhai, 
this is a very nice code. i want to populate district in first select box and based in district i want to select upozilla. below my tables.

  `distName` varchar(250) NOT NULL,
  PRIMARY KEY (`distID`)

  `upoName` varchar(250) NOT NULL,
  `distID` int(11) NOT NULL,

how can i load it rather than using array??