Kishan Kumar

Kishan Kumar

  • NA
  • 127
  • 27.4k

How to selected item change function with database in php.

Jun 5 2018 1:39 AM
Hi, Sir
 
Please Resolve the code
 
How to selected item change function with database in php.
 
First_page-: index.php
  1. <html>  
  2. <head>  
  3. <title>Drop_Down</title>  
  4. <script>  
  5. function getState(countryId) {  
  6. var strURL="findState.php?country="+countryId;  
  7. var req = getXMLHTTP();  
  8. if (req) {  
  9. req.onreadystatechange = function() {  
  10. if (req.readyState == 4) {  
  11. // only if "OK"  
  12. if (req.status == 200) {  
  13. document.getElementById('statediv').innerHTML=req.responseText;  
  14. document.getElementById('citydiv').innerHTML='<select name="city">'+  
  15. '<option>Select City</option>'+'</select>';  
  16. else {  
  17. alert("Problem while using XMLHTTP:\n" + req.statusText);  
  18. }  
  19. }  
  20. }  
  21. req.open("GET", strURL, true);  
  22. req.send(null);  
  23. }  
  24. }  
  25. </script>  
  26. <script>  
  27. function getCity(countryId,stateId) {  
  28. var strURL="findCity.php?country="+countryId+"&state="+stateId;  
  29. var req = getXMLHTTP();  
  30. if (req) {  
  31. req.onreadystatechange = function() {  
  32. if (req.readyState == 4) {  
  33. // only if "OK"  
  34. if (req.status == 200) {  
  35. document.getElementById('citydiv').innerHTML=req.responseText;  
  36. else {  
  37. alert("Problem while using XMLHTTP:\n" + req.statusText);  
  38. }  
  39. }  
  40. }  
  41. req.open("GET", strURL, true);  
  42. req.send(null);  
  43. }  
  44. }  
  45. </script>  
  46. </head>  
  47. <body>  
  48. <form method="post" action="" name="form1">  
  49. <center>  
  50. <h1>Drop Down Select Item Change</h1>  
  51. <table width="45%" cellspacing="0" cellpadding="0">  
  52. <tr>  
  53. <td width="175">Country</td>  
  54. <td width="150">:</td>  
  55. <td width="150">  
  56. <select name="country" onChange="getState(this.value)">  
  57. <option value="">Select Country</option>  
  58. <?php while ($row=mysql_fetch_array($result)) { ?>  
  59. <option value=<?php echo $row['id']?>><?php echo $row['country']?></option>  
  60. <?php } ?>  
  61. </select>  
  62. </td>  
  63. </tr>  
  64. <tr>  
  65. <td>State</td>  
  66. <td width="50">:</td>  
  67. <td >  
  68. <div id="statediv">  
  69. <select name="state" >  
  70. <option>Select State</option>  
  71. </select>  
  72. </div>  
  73. </td>  
  74. </tr>  
  75. <tr>  
  76. <td>City</td>  
  77. <td width="50">:</td>  
  78. <td>  
  79. <div id="citydiv">  
  80. <select name="city">  
  81. <option>Select City</option>  
  82. </select>  
  83. </div>  
  84. </td>  
  85. </tr>  
  86. </table>  
  87. </br></br></br></br>  
  88. <input type="submit" name="submit"/>  
  89. </center>  
  90. </form>  
  91. </body>  
  92. </html>  
Second:-findcity.php
  1. <?php  
  2. $countryId = intval($_GET['country']);  
  3. $stateId = intval($_GET['state']);  
  4. $con = mysql_connect('localhost''root''','test');  
  5. if (!$con) {  
  6. die('Could not connect: ' . mysql_error());  
  7. }  
  8. mysql_select_db('test');  
  9. $query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";  
  10. $result=mysql_query($query);  
  11. ?>  
  12. <select name="city">  
  13. <option>Select City</option>  
  14. <?php while($row=mysql_fetch_array($result)) { ?>  
  15. <option value=<?php echo $row['id']?>><?php echo $row['city']?></option>  
  16. <?php } ?>  
  17. </select>  
  18. Thirdpage:-findstate.php  
  19. <?php  
  20. $country = intval($_GET['country']);  
  21. $con = mysql_connect('localhost''root''','test');  
  22. if (!$con) {  
  23. die('Could not connect: ' . mysql_error());  
  24. }  
  25. mysql_select_db('test');  
  26. $query = "SELECT id,statename FROM state WHERE countryid='$country'";  
  27. $result = mysql_query($query);  
  28. ?>  
  29. <select name="state" onchange="getCity(<?php echo $country?>,this.value)">  
  30. <option>Select State</option>  
  31. <?php while ($row=mysql_fetch_array($result)) { ?>  
  32. <option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>  
  33. <?php } ?>  
  34. </select>  
I have attach the all code
 
Thanks
(Kishan)