Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <style type="text/css">
  5. .select-boxes{width: 280px;text-align: center;}
  6.  
  7. </style>
  8. <script src="jquery.min.js"></script>
  9. <script type="text/javascript">
  10. $(document).ready(function(){
  11. $('#country').on('change',function(){
  12. var countryID = $(this).val();
  13. if(countryID){
  14. $.ajax({
  15. type:'POST',
  16. url:'ajaxData.php',
  17. data:'country_id='+countryID,
  18. success:function(html){
  19. $('#state').html(html);
  20. $('#city').html('<option value="">Select state first</option>');
  21. }
  22. });
  23. }else{
  24. $('#state').html('<option value="">Select country first</option>');
  25. $('#city').html('<option value="">Select state first</option>');
  26. }
  27. });
  28.  
  29. $('#state').on('change',function(){
  30. var stateID = $(this).val();
  31. if(stateID){
  32. $.ajax({
  33. type:'POST',
  34. url:'ajaxData.php',
  35. data:'state_id='+stateID,
  36. success:function(html){
  37. $('#city').html(html);
  38. }
  39. });
  40. }else{
  41. $('#city').html('<option value="">Select state first</option>');
  42. }
  43. });
  44. });
  45. </script>
  46. </head>
  47. <body>
  48. <form id="form1" name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
  49. <?php
  50. //Include database configuration file
  51. include('dbConfig.php');
  52.  
  53. //Get all country data
  54. $query = $db->query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC");
  55.  
  56. //Count total number of rows
  57. $rowCount = $query->num_rows;
  58. ?>
  59. <select name="country" id="country">
  60. <option value="">Select Country</option>
  61. <?php
  62. if($rowCount > 0){
  63. while($row = $query->fetch_assoc()){
  64. echo '<option value="'.$row['country_id'].'">'.$row['country_name'].'</option>';
  65. }
  66. }else{
  67. echo '<option value="">Country not available</option>';
  68. }
  69. ?>
  70. </select>
  71.  
  72. <select name="state" id="state">
  73. <option value="">Select country first</option>
  74. </select>
  75.  
  76. <select name="city" id="city">
  77. <option value="">Select state first</option>
  78. </select>
  79. <input type="submit" name="Submit" id="Submit" value="Submit" />
  80. </form>
  81. </body>
  82. </html>
  83.  
  84. <?php
  85. //Include database configuration file
  86. include('dbConfig.php');
  87.  
  88. if(isset($_POST["country_id"]) && !empty($_POST["country_id"])){
  89. //Get all state data
  90.  
  91. $query = $db->query("SELECT * FROM states WHERE country_id IN (".$_POST['country_id'].")");
  92.  
  93. //Count total number of rows
  94. $rowCount = $query->num_rows;
  95.  
  96. //Display states list
  97. if($rowCount > 0){
  98. echo '<option value="">Select state</option>';
  99. while($row = $query->fetch_assoc()){
  100. echo '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
  101. }
  102. }else{
  103. echo '<option value="">State not available</option>';
  104. }
  105. }
  106.  
  107. if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
  108. //Get all city data
  109. $query = $db->query("SELECT * FROM cities WHERE state_id IN(".$_POST["state_id"].")");
  110.  
  111. //Count total number of rows
  112. $rowCount = $query->num_rows;
  113.  
  114. //Display cities list
  115. if($rowCount > 0){
  116. echo '<option value="">Select city</option>';
  117. while($row = $query->fetch_assoc()){
  118. echo '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
  119. }
  120. }else{
  121. echo '<option value="">City not available</option>';
  122. }
  123. }
  124. ?>
  125.  
  126. <?php
  127. //db details
  128. $dbHost = 'localhost';
  129. $dbUsername = 'root';
  130. $dbPassword = '';
  131. $dbName = 'location_db';
  132.  
  133. //Connect and select the database
  134. $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
  135.  
  136. if ($db->connect_error) {
  137. die("Connection failed: " . $db->connect_error);
  138. }
  139. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement