|
|
|
|
|
|
| |
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> | | |
|
| Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | 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 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 | | | 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 | | | 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 | | | 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 | | | 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 | | | 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 | | | Newbie Notes #9 - Hyperlinking a post Categories : PHP, Java Script, HTML and PHP, Beginner Guides | | | PHP4 session helper HTML file.
Categories : PHP, Java Script, HTML and PHP, Sessions | | | Dynamic Calender in PHP, Javascript and HTML. Categories : PHP, Java Script, HTML and PHP, Calendar | | | Validator 98 - a PHP-script to generate form-validation-code in JavaScript. Categories : Complete Programs, Java Script, PHP, HTML and PHP | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | |
|
|
|