dongqiyou0303 2013-11-16 11:46
浏览 32

3使用ajax和php下拉

There is 3 dropdown for country, state and district. I was doing when i select a country from country dropdown then state dropdown shows only adjacent to the country and when i select state then district dropdown shows only adjacent district to the state drop down.

My home.php code is

<?php
include "config.php";
?>
<html> 
<head> 
<script type="text/javascript">
        function showstate(str){
            if (str == "") {
                document.getElementById("state").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("state").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "state.php?country=" + str, true);
            xmlhttp.send();
        }
        function showcity(str2){
            if (str2 == "") {
                document.getElementById("city").innerHTML = "";
                return;
            }
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("city").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "district.php?state=" + str2, true);
            xmlhttp.send();
        }
 </script>
</head>
<body> 
<form > 
Choose your country : <select name="country"  onchange="showstate(this.value)"> <option>Select country  </option> 
 <?php

   $query = "SELECT DISTINCT country FROM area";
   $result = mysql_query($query);
   while($row = mysql_fetch_array($result)){
   echo"<option  value =".$row[0]."> ".$row[0]."</option>";
                                }   
 ?>
</select>
State : <select id="state" name="state" onChange="showcity(this.value)"> <option>Select state</option> </select> 
District : <select id="city" name="city"> <option>Select district </option> </select>
</form>
</body>
</html>

and state.php is

<?php
include "config.php";
echo " <option>Select state  </option> " ;
if(isset( $_GET['country']) ) 
{
  $country = $_GET['country'];
}
  $query = "SELECT DISTINCT state FROM area WHERE country = '".$country."' "; 
  $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) 
    {
           echo"<option  value =".$row['state']."> ".$row['state']."</option>";
    }
?>

And district.php is

<?php
include "config.php";
echo " <option>Select district  </option> " ;
if(isset( $_GET['state']) ) 
{
  $state = $_GET['state'];
}
   $query = "SELECT DISTINCT district FROM area WHERE state = '".$state."' "; 
   $result = mysql_query($query);
   while ($row = mysql_fetch_assoc($result)) 
   {

      echo"<option  value =".$row['district']."> ".$row['district']."</option>";

   }
 ?>

Everything is ok. But the problem is that using all three dropdown when i select any district, if again in the same page i m going to reselect country from the country dropdown then district does not remains changed.

  • 写回答

1条回答 默认 最新

  • dpjs2005 2013-11-16 11:56
    关注

    On Change of country and just before calling ur ajax call to load states, 1st make district combo empty.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图