By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

updating country,city,state in drop down box

P: 60
Hi

In my php page , there is a user registration form. Here the user has to select
the country, state, city from the drop down box. How this can be handled in php?
If a country is selected in a drop down box , its corresponding states should be
populated in the state drop down box. If a state is selected , its corresponding
cities should be populated in the city drop down box. Where all these data has to be stored and retrieved. What type of data storage and datastructure can be used. ? How this can be done in php + javascript ?

Thanks in advance
somaskarthic
Aug 30 '06 #1
Share this Question
Share on Google+
10 Replies


P: 60
Hi

In my php page , there is a user registration form. Here the user has to select
the country, state, city from the drop down box. How this can be handled in php?
If a country is selected in a drop down box , its corresponding states should be
populated in the state drop down box. If a state is selected , its corresponding
cities should be populated in the city drop down box. Where all these data has to be stored and retrieved. What type of data storage and datastructure can be used. ? How this can be done in php + javascript ?

Thanks in advance
somaskarthic
Aug 30 '06 #2

100+
P: 268
Hi friend using AJAX functionality Its very easy to complete ur task.
I will send sample code to later using AJAX concept to drop down select option
Aug 30 '06 #3

P: 60
Thanks . Please send the AJAX code .


Hi friend using AJAX functionality Its very easy to complete ur task.
I will send sample code to later using AJAX concept to drop down select option
Aug 30 '06 #4

100+
P: 268
Here is the ajax code this code i call one php file
This file u write your coading
Expand|Select|Wrap|Line Numbers
  1.  
  2.     if(window.XMLHttpRequest)
  3.         {
  4.             http=new XMLHttpRequest();
  5.         }
  6.         else if(window.ActiveXObject)
  7.         {
  8.             http=new ActiveXObject("Microsoft.XMLHTTP");
  9.         }
  10.  
  11.         var url = "countrylist.php?cid="+search_program.value;
  12.         //alert(url);  
  13.         //mlsaddress=address;
  14.         http.open("GET", url , true);
  15.  
  16.         http.onreadystatechange = countryprocess;
  17.  
  18.         http.send(null);
  19. //==jai            
  20.     }
  21. }
  22.  
  23. function countryprocess()
  24. {
  25.     if (http.readyState == 4) {
  26.     // Split the comma delimited response into an array
  27.         if(http.status==200)
  28.         {  alert(http.responseText);
  29.  
  30.  
  31.  
  32.             if(http.responseText!="No values")
  33.             {
  34.                 var clist=http.responseText.split("_");
  35.                 for(i=0;i<clist.length;i++)
  36.                 {
  37.                     var myclist=clist[i].split(",");
  38.                     document.getElementById("search_country").options[i+1]=new Option(myclist[1],myclist[0]);
  39.                 }
  40.             }
  41.  
  42.  
  43.         }
  44.  
  45.   }
  46. }
  47.  
Aug 30 '06 #5

P: 60
Hi

My web server is Fedora Core 4 Linux. If i use window.ActiveXObjects (for Ajax) , will it work ?

Please post your reply
-somaskarthic
Aug 31 '06 #6

100+
P: 268
Yes I hope its working My prodection serevr are linux machine Now working well

So you try to implement the code.
Aug 31 '06 #7

unicorn
P: 23
here u go, u have to change group into whatever u want. in groups there is specified array of groups. it is very simple

---> experim.php



Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4.   <title></title>
  5. </head>
  6.  
  7. <body>
  8.  
  9. <SCRIPT language=JavaScript>
  10. function reload(form)
  11. {
  12. // Setting the variable with the value of selected country's ID
  13. var val=populate.countries.options[populate.countries.options.selectedIndex].value;
  14. self.location='experim.php?countryId=' + val;
  15. var val2=val+'&cityID='+populate.cities.options[populate.cities.options.selectedIndex].value;
  16. self.location='experim.php?countryId=' + val2;
  17. var val3=val2+'&groupID='+populate.groups.options[populate.groups.options.selectedIndex].value;
  18. self.location='experim.php?countryId=' + val3;
  19. }
  20. JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");
  21. </script>
  22.  
  23.  
  24. <?php
  25. function getCountryList()
  26. {  $countries     = array (
  27.                           '1' => 'Bangladesh',
  28.                           '2' => 'USA',
  29.                           '3' => 'UK'
  30.                           );
  31.  
  32.   return $countries;
  33. }
  34.  
  35.  
  36. function getCityList($countryId)
  37. {
  38.   // City list array
  39.   // First key of the array is the Country ID, which holds an array of City list
  40.   $cities       = array (
  41.                           'Bangladesh' => array ('Dhaka', 'Chittagong', 'What else'),
  42.                           'UK' => array ('London', 'Cannot Remember'),
  43.                           'USA' => array ('Washington', 'N.Y.', 'etc')
  44.                           );
  45.  
  46.   return $cities[$countryId];
  47. }
  48.  
  49.  
  50. function getGroupList($cityID)
  51. {
  52.   $groups       = array (
  53.  
  54.                           'etc' => array ('aka', 'Chg', ' else'),
  55.                           'Washington' => array ('ndon', 'Cnnot member'),
  56.                           'N.Y.' => array ('ashington', 'N.Y.', 'et')
  57.                //list of groups
  58.  
  59.                           );
  60.  
  61.  
  62.   return $groups[$cityID];
  63. }
  64.  
  65.  
  66.  
  67. ?>
  68. <form action="experim.php" name="populate">
  69. <?
  70.  
  71. // Retrieving the country list
  72. $countries  = getCountryList();
  73.  
  74. // Setting the variable if the country is selected for its city list
  75. @$countryId  = $_GET['countryId'];
  76. @$cityID = $_GET['cityID'];
  77. // Retrieving the city list if a country is selected
  78. $cities   = ($countryId) ? getCityList($countryId) : null;
  79. $groups   = ($cityID) ? getGroupList($cityID) : null;
  80.  
  81. if (!empty($countries))
  82. {
  83.   // Generating the country drop down menu
  84.   echo "<select onChange='reload(this.form)' name='countries'>";
  85.   foreach ($countries as  $value)
  86.   {
  87.     echo "<option value='$value'";
  88.  
  89.     if ($countryId == $value)
  90.       echo "selected";
  91.  
  92.     echo ">$value</option>";
  93.   }
  94.   echo "</select>";
  95. }
  96.  
  97. if (!empty($cities))
  98. {
  99.   // Generating the city drop down menu if a country is selected
  100.   echo "<select onChange='reload(this.form)' name='cities'>";
  101.   foreach ($cities as $value)
  102.   {
  103.     echo "<option value='$value'";
  104.  
  105.     if ($cityID == $value)
  106.       echo "selected";
  107.  
  108.     echo ">$value</option>";
  109.   }
  110.   echo "</select>";
  111. }
  112.  
  113.  
  114. if (!empty($groups))
  115. {
  116.   // Generating the city drop down menu if a country is selected
  117.   echo "<select onChange='reload(this.form)' name='groups'>";
  118.   foreach ($groups as $value)
  119.   {
  120.     echo "<option value='$value'";
  121.  
  122.     if ($groupID == $value)
  123.       echo "selected";
  124.  
  125.     echo ">$value</option>";
  126.   }
  127.   echo "</select>";
  128. }
  129.  
  130.  
  131.  
  132.  
  133. if (!empty($groups))
  134. {
  135.   // Generating the city drop down menu if a country is selected
  136.   echo "<select name='groups'>";
  137.   foreach ($groups as $key=>$value)
  138.   {
  139.     echo "<option value='$key'>$value</option>";
  140.   }
  141.   echo "</select>";
  142. }
  143.  
  144.  
  145. ?>
  146.  
  147. </body>
  148.  
  149. </html>
Sep 27 '06 #8

P: 1
Hi friend using AJAX functionality Its very easy to complete ur task.
I will send sample code to later using AJAX concept to drop down select option
elo there, is it okay if you send also to me the complete code... just for updating country, citym state.. please.. thank you so much..
Oct 2 '06 #9

P: 2
I am also facing problem in making
the country, state, city drop down selct box.
but fails

if u have any plz send me

thax
Nov 25 '11 #10

acoder
Expert Mod 15k+
P: 16,027
Post your code attempt describing what problems you're having and I can split it off into its own thread.
Nov 25 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.