-2

I am creating a project .in this i want to get address through dropdownlist,if i select country from dropdownlist then another dropdownlist will apper which has the states of crresponding country.same as with state and so on till requerement?

  • 3
    A question mark does not make a statement into a question. Do you want to ask something? – lanzz Jun 13 '12 at 11:59
  • @lanzz -i already told how to get runtime control..... – Rahul Jaiswal Jun 13 '12 at 13:48
  • possible duplicate of [PHP drop down which each are dependable](http://stackoverflow.com/q/2680289/), [Cascading Dropdown List](http://stackoverflow.com/q/2776415/), [How can a click of a button change the content of a drop down menu?](http://stackoverflow.com/q/1131766/), [Dropdowns with 10 thousand possible values and sequence-important dropdowns vs. graceful degredation](http://stackoverflow.com/q/2239865/). – outis Jun 14 '12 at 08:01

2 Answers2

1

You're looking for a "cascading dropdown". Effectively, your first dropdown, upon selection makes an ajax call which upon success populates the second dropdown and so on.

It's a very common feature (probably the most common along with ajax autocomplete) and a google or two will find you some really solid examples.

I'd strongly suggest using jQuery for such an endeavour.

Jeff Watkins
  • 6,343
  • 16
  • 19
0
  <select name="country" onchange="showstate(this.value);">

//script code
 function showstate(ctr)
  {
if (ctr==" ")
{
    document.getElementById("txtHint").innerHTML="";
    return;
}
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var x=xmlhttp.responseText;
        var arr = new Array();
        var v=new Array();
        arr=x.split(",");
        // Create an Option object        
        document.getElementById("DropDownList").innerHTML='<option value="select">Select university</option>';
        for (var i=0;i<arr.length-1;i=i+2)
        {
            v[i]=arr[i];
            v[i+1]=arr[i+1];
            document.getElementById("DropDownList").innerHTML+="<option value="+v[i]+">"+v[i+1]+"</option>";
        }
       }
      }
   xmlhttp.open("GET","getstate.php?q="+ctr,true);
   xmlhttp.send();
  }

// getstate.php

  <?php

     $q=$_GET["q"];
     $sql="SELECT DISTINCT `state` FROM `tbname` WHERE `country` = '$q'";
     $result=mysql_query($sql);
     $str='';
     while($row = mysql_fetch_array($result))
   {
    $str.=$row['country'].",";
   }
     echo $str;
?>
chaitu
  • 599
  • 1
  • 6
  • 13