populate dropdown on selection of other dropdown

前端 未结 4 600
一整个雨季
一整个雨季 2020-12-12 05:32

I have two drop-down boxes. I want to populate second drop-down box on selection in the first drop-down box. My values are retrieving from database. Is this possible withou

4条回答
  •  借酒劲吻你
    2020-12-12 05:49

    Lets take an easy example, I'm using this for the same purpose that you want and it works perfectly fine.

    This is the country dropdown:

    ";
            while($clist=mysql_fetch_array($countrylist))
            {
              echo ""."
    "; } echo ""; ?>

    This is the region dropdown:

    
    

    Now make a seperate file named crlist.js and include it in the page having above code like this:

    
    

    code for crlist.js:

    var request = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    request = false;
    }
    }
    @end @*/
    function fillSelect(country,path) {
    var url = path+"crlist.php?country=" + country;
    request.open("GET", url, true);
    request.onreadystatechange = go;
    request.send(null);
    }
    
    function go() {
    if (request.readyState == 4) {
    //if (request.status == 200) {
    
    var response = request.responseText;
    
    var list=document.getElementById("region");
                for (i = list.length - 1; i>=0; i--) {
                    list.remove(i);
                }
    var records=response.split('|');
    for (i=1; i=0; i--) {
                    list.remove(i);
                }
            //while (list.childNodes[0]) {
            //list.removeChild(list.childNodes[0]);
            //}
            }
            fillSelect(this.value,path);
            //alert(this.value);
    
        }
    //fillSelect(country.value);
    }
    

    Now make a seperate file named crlist.php.

    Code for crlist.php:

    ".$region['Name']."";
    }       
    ?>
    

    Now add following script on the page having dropdowns:

    
    
    

    This is my own script, and i've assumed that you have created country and region tables. But you need to tweak the queries and above code according to your db structure.

    Reference to my answer: Cascade Dropdown List using jQuery/PHP

    Hope this helps.

提交回复
热议问题