Advertisement
chiab_gigi

index.php

Sep 6th, 2019
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 KB | None | 0 0
  1.     </select>
  2.     <select id="city-select">
  3.  
  4.     </select>
  5.  
  6.     <div id="result"><?php echo $row['city_cap'] ?></div>
  7.  
  8.     <script type="text/javascript">
  9.         function getStatesSelectList(){
  10.             var country_select = document.getElementById("country-select");
  11.             var city_select = document.getElementById("city-select");
  12.  
  13.             var country_id = country_select.options[country_select.selectedIndex].value;
  14.             console.log('Country_id : ' + country_id);
  15.  
  16.             var xhr = new XMLHttpRequest();
  17.             var url = 'states.php?country_id=' + country_id;
  18.             // open function
  19.             xhr.open('GET', url, true);
  20.             xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  21.  
  22.             // check response is ready with response states = 4
  23.             xhr.onreadystatechange = function(){
  24.                 if(xhr.readyState === 4 && xhr.status === 200){
  25.                     var text = xhr.responseText;
  26.                     //console.log('response from states.php : ' + xhr.responseText);
  27.                     var state_select = document.getElementById("state-select");
  28.                     state_select.innerHTML = text;
  29.                     state_select.style.display='inline';
  30.                     city_select.style.display='none';
  31.                 }
  32.             }
  33.  
  34.             xhr.send();
  35.         }
  36.  
  37.         function getCitySelectList(){
  38.             var state_select = document.getElementById("state-select");
  39.  
  40.             var state_id = state_select.options[state_select.selectedIndex].value;
  41.             console.log('State_id : ' + state_id);
  42.  
  43.             var xhr = new XMLHttpRequest();
  44.             var url = 'cities.php?state_id=' + state_id;
  45.             // open function
  46.             xhr.open('GET', url, true);
  47.             xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  48.  
  49.             // check response is ready with response states = 4
  50.             xhr.onreadystatechange = function(){
  51.                 if(xhr.readyState === 4 && xhr.status === 200){
  52.                     var text = xhr.responseText;
  53.                     //console.log('response from cities.php : ' + xhr.responseText);
  54.                     var city_select = document.getElementById("city-select");
  55.                     city_select.innerHTML = text;
  56.                     city_select.style.display='inline';
  57.                 }
  58.             }
  59.  
  60.             xhr.send();
  61.         }
  62.  
  63.         var country_select = document.getElementById("country-select");
  64.         country_select.addEventListener("change", getStatesSelectList);
  65.  
  66.         var state_select = document.getElementById("state-select");
  67.         state_select.addEventListener("change", getCitySelectList);
  68.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement