Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Alterando o próximo valor da lista suspensa com o evento onchange da lista suspensa anterior

<td>Country</td>
<td>
      <select  id="country" onChange="getState(this.value)" name="country">
                            <option value="">All</option>
                            <option value="1">India</option>
                            <option value="2">China</option>

      </select>
</td>


function getState(str){
  if(str=='All'){
    document.getElementById("state").innerHTML="";     
  }else{
    document.getElementById("state").innerHTML="<img src='<?php echo $serverimage?>ajax-loader.gif' />";
        if(window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
    }
    else
    {
     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?countryid="+str,true);
    xmlhttp.send();
   }//ELSE ENDS
}//FUNCTION ENDS

//Estado.php
<?php
  $country=$_GET["country"];  
  /*
    code to fetch all states from database with $country and fetch in variable $states
    Fetch records based on value passed in country dropw down. ie id or countryname 
  */
    echo '<select name="state" id="state">';
   foreach($states as $state)                
     echo '<option value="'.$state.'">'.$state.'</option>';            

   echo '<option value="Other">Other</option>'; 
   echo '</select>';
   exit;    

?>

Eu não testei este código, então pode haver alguns erros bobos, então tome cuidado com isso.