Hi, Sir
Please Resolve the code
How to selected item change function with database in php.
First_page-: index.php
- <html>
- <head>
- <title>Drop_Down</title>
- <script>
- function getState(countryId) {
- var strURL="findState.php?country="+countryId;
- var req = getXMLHTTP();
- if (req) {
- req.onreadystatechange = function() {
- if (req.readyState == 4) {
-
- if (req.status == 200) {
- document.getElementById('statediv').innerHTML=req.responseText;
- document.getElementById('citydiv').innerHTML='<select name="city">'+
- '<option>Select City</option>'+'</select>';
- } else {
- alert("Problem while using XMLHTTP:\n" + req.statusText);
- }
- }
- }
- req.open("GET", strURL, true);
- req.send(null);
- }
- }
- </script>
- <script>
- function getCity(countryId,stateId) {
- var strURL="findCity.php?country="+countryId+"&state="+stateId;
- var req = getXMLHTTP();
- if (req) {
- req.onreadystatechange = function() {
- if (req.readyState == 4) {
-
- if (req.status == 200) {
- document.getElementById('citydiv').innerHTML=req.responseText;
- } else {
- alert("Problem while using XMLHTTP:\n" + req.statusText);
- }
- }
- }
- req.open("GET", strURL, true);
- req.send(null);
- }
- }
- </script>
- </head>
- <body>
- <form method="post" action="" name="form1">
- <center>
- <h1>Drop Down Select Item Change</h1>
- <table width="45%" cellspacing="0" cellpadding="0">
- <tr>
- <td width="175">Country</td>
- <td width="150">:</td>
- <td width="150">
- <select name="country" onChange="getState(this.value)">
- <option value="">Select Country</option>
- <?php while ($row=mysql_fetch_array($result)) { ?>
- <option value=<?php echo $row['id']?>><?php echo $row['country']?></option>
- <?php } ?>
- </select>
- </td>
- </tr>
- <tr>
- <td>State</td>
- <td width="50">:</td>
- <td >
- <div id="statediv">
- <select name="state" >
- <option>Select State</option>
- </select>
- </div>
- </td>
- </tr>
- <tr>
- <td>City</td>
- <td width="50">:</td>
- <td>
- <div id="citydiv">
- <select name="city">
- <option>Select City</option>
- </select>
- </div>
- </td>
- </tr>
- </table>
- </br></br></br></br>
- <input type="submit" name="submit"/>
- </center>
- </form>
- </body>
- </html>
Second:-findcity.php
- <?php
- $countryId = intval($_GET['country']);
- $stateId = intval($_GET['state']);
- $con = mysql_connect('localhost', 'root', '','test');
- if (!$con) {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db('test');
- $query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
- $result=mysql_query($query);
- ?>
- <select name="city">
- <option>Select City</option>
- <?php while($row=mysql_fetch_array($result)) { ?>
- <option value=<?php echo $row['id']?>><?php echo $row['city']?></option>
- <?php } ?>
- </select>
- Thirdpage:-findstate.php
- <?php
- $country = intval($_GET['country']);
- $con = mysql_connect('localhost', 'root', '','test');
- if (!$con) {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db('test');
- $query = "SELECT id,statename FROM state WHERE countryid='$country'";
- $result = mysql_query($query);
- ?>
- <select name="state" onchange="getCity(<?php echo $country?>,this.value)">
- <option>Select State</option>
- <?php while ($row=mysql_fetch_array($result)) { ?>
- <option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>
- <?php } ?>
- </select>
I have attach the all code
Thanks
(Kishan)