Dynamic Dropdown Box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    Dynamic Dropdown Box

    I want to have a dynamic dropdown box whose entries would depend on the selection of an entry in the first dropdown box. BUT the second dropdown box should not reload, only the entries inside should get refreshed or reloaded.

    i have a piece of code to create a dynamic dropdown box from w3schools.com but it has the problem of reloading the second dropdown box as well. Please see my code:
    Code:
    //index.php
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    var xmlHttp;function showUser(str)
    { 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request");
     return;
     }
    var url="getdept.php";
    url=url+"?c="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
     } 
    }function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    </script>
    </head>
    <body><form> 
    Select a User:
    <select name="category" onchange="showUser(this.value)">
    <option value="Ministry">Ministry</option>
    <option value="Department">Department</option>
    <option value="District">District</option>
    <option value="Corporation">Corporation</option>
    <option value="NGOs">NGOs</option>
    <option value="Others">Others</option>
    </select>
    </form><p>
    <div id="txtHint"><b>&nbsp;</b></div>
    </p></body>
    </html>
    
    //getdept.php
    <?php
    $c=$_GET["c"];
    
    $con = mysql_connect('localhost', 'root', 'root');
    if (!$con)
     {
     die('Could not connect: ' . mysql_error());
     }
    
    mysql_select_db("db", $con);
    
    $sql="SELECT * FROM tblagency WHERE category = '".$c."'";
    
    $result = mysql_query($sql);
    echo "<select name=\"agency\">";
    while($row = mysql_fetch_array($result))
     {
    
    echo "<option value=".$row['id'].">".$row['agency']."</option>";
    
    }
    echo "</select>";
    mysql_close($con);
    ?>
    So, as shown above in the <div id="txtHint"><b >&nbsp;</b></div> area i want to have a pre-present dropdown box and when the uses selects an entry in the first dropdown box, the second dropdown box entries should be changed but not the dropdown box as a whole. Please advice me!
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #2
    so, if i get it right, all you want to do is select a value from a select using javascript. am i correct?

    you might concider looking it up on google. i got 17.100.000 hits so i guess you'll find what you need.

    from what i saw, there isn't just one function to select the appropriate value. you'll have to loop through it and find the value you're looking for. then, you can set the value by the .selectedIndex property.

    to change the options in the selectbox, you'll probably have to add and delete the options. this can be done by appendChild and removeChild.

    i hope that helped

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      First remove the current options:
      Code:
      selObj.options.length = 0;
      then add the options (using a loop)
      Code:
      selObj.options[i] = new Option(text, value);
      This means you will need to change the PHP code to echo appropriate output.

      Comment

      Working...