WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
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
PHPClasses
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.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 : 90159
File : 4398.zip
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
@Web: http://ehsan.bdwebwork.com

<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 ;
}
</script>

<?php

/*
- 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'
                         
);
 
  return
$countryList;
}

/*
- 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')
                          );
 
  return
$cityList[$countryId];
}
?>

<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(
$countryList))
{
 
// Generating the country drop down menu
 
echo "<select onChange='reload(this.form)' name='countryList'>";
  foreach (
$countryList as $key => $value)
  {
    echo
"<option value='$key'";
   
    if (
$countryId == $key)
      echo
"selected";
   
    echo
">$value</option>";
  }
  echo
"</select>";
}

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

?>
</form>



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
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
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
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
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
PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions
Newbie Notes #9 - Hyperlinking a post
Categories : PHP, Java Script, HTML and PHP, Beginner Guides
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
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
PHP based HTML rabbing Tools
Categories : PHP, HTML and PHP, Tag Extractors, Regexps, Beginner Guides
A simple php file uploader
Categories : PHP, Filesystem, 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.

Thanks
 
 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.

CREATE TABLE IF NOT EXISTS `district` (
  `distID` int(11) NOT NULL AUTO_INCREMENT,
  `distName` varchar(250) NOT NULL,
  PRIMARY KEY (`distID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

CREATE TABLE IF NOT EXISTS `upozilla` (
  `upoID` int(11) NOT NULL AUTO_INCREMENT,
  `upoName` varchar(250) NOT NULL,
  `distID` int(11) NOT NULL,
  PRIMARY KEY (`upoID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;

how can i load it rather than using array??