Hi again,
Here's an example using arrays.
$array_main = array(
91 => array("Andra Pradesh" => array("Hyderabad","Vishakhapatnam","Tirupati"),
"Assam" => array(),"New Delhi" => array(),"Punjab" => array(),
"Haryana" => array(),"Tamil Nadu" => array(),"Kerala" => array(),"Utter Pradesh" => array()),
1 => array("California" => array(),"Texas" => array(),"Alaska" => array(),
"Massachusetts" => array(),"Virginia" => array(),"Colorado" => array()),
61 => array("South New Wales" => array(), "Victoria" => array(),
"Queensland" => array(), "Tasmania" => array())
);
$country_code = $_GET['countryCode'];
$state = $_GET['stateName'];
if(!$country_code) return false;
else {
$array_state = $array_main[$country_code];
if(!$state) {
foreach ($array_state as $key=>$value) {
echo '
<option value="'.$key.'">'.$key.'</option>';
';
}
return true;
}
else {
// Store the Cities also as array
$array_cities = $array_main[$country_code][$state];
foreach ($array_cities as $value) { // Now we don't have any further nesting
echo '
<option value="'.$value.'">'.$value.'</option>';
';
}
return true;
}
}
?>
The Front end will be changed and will use some kind of method that I am suggesting here.
$(document).ready(function(){
$('#selectbox_country').change(function(){
queryString = '?countryCode=' + encodeURIComponent($(this).val()) + '&stateName=';
$('#selectbox_state').load('http://www.example.com/script.php' + queryString);
});
$('#selectbox_state').change(function(){
queryString = '?countryCode=' + encodeURIComponent($('#selectbox_country').val()) + '&stateName='
+ encodeURIComponent($(this).val());
$('#selectbox_cities').load('http://www.example.com/script.php' + queryString);
});
$('#selectbox_cities').change(function(){
});
});
I hope this example will help you understand the concept. Example though wasn't a well written code. But, you get the idea right.
You can have a look at a similar code of mine on my test website
here
Open the source code of given link and read from line 205 ahead. That's a more advanced jQuery implementation of a similar concept.
You are free to copy and modify the code according to your need if you wish to...
With Regards
Tushar Srivastava