469,325 Members | 1,520 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,325 developers. It's quick & easy.

Dynamic Dropdown Box

107 100+
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:
Expand|Select|Wrap|Line Numbers
  1. //index.php
  2. <html>
  3. <head>
  4. <script language="javascript" type="text/javascript">
  5. var xmlHttp;function showUser(str)
  6. xmlHttp=GetXmlHttpObject();
  7. if (xmlHttp==null)
  8.  {
  9.  alert ("Browser does not support HTTP Request");
  10.  return;
  11.  }
  12. var url="getdept.php";
  13. url=url+"?c="+str;
  14. url=url+"&sid="+Math.random();
  15. xmlHttp.onreadystatechange=stateChanged;
  16. xmlHttp.open("GET",url,true);
  17. xmlHttp.send(null);
  18. }function stateChanged() 
  19. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  20.  { 
  21.  document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  22.  } 
  23. }function GetXmlHttpObject()
  24. {
  25. var xmlHttp=null;
  26. try
  27.  {
  28.  // Firefox, Opera 8.0+, Safari
  29.  xmlHttp=new XMLHttpRequest();
  30.  }
  31. catch (e)
  32.  {
  33.  //Internet Explorer
  34.  try
  35.   {
  36.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  37.   }
  38.  catch (e)
  39.   {
  40.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  41.   }
  42.  }
  43. return xmlHttp;
  44. }
  45. </script>
  46. </head>
  47. <body><form> 
  48. Select a User:
  49. <select name="category" onchange="showUser(this.value)">
  50. <option value="Ministry">Ministry</option>
  51. <option value="Department">Department</option>
  52. <option value="District">District</option>
  53. <option value="Corporation">Corporation</option>
  54. <option value="NGOs">NGOs</option>
  55. <option value="Others">Others</option>
  56. </select>
  57. </form><p>
  58. <div id="txtHint"><b>&nbsp;</b></div>
  59. </p></body>
  60. </html>
  61.  
  62. //getdept.php
  63. <?php
  64. $c=$_GET["c"];
  65.  
  66. $con = mysql_connect('localhost', 'root', 'root');
  67. if (!$con)
  68.  {
  69.  die('Could not connect: ' . mysql_error());
  70.  }
  71.  
  72. mysql_select_db("db", $con);
  73.  
  74. $sql="SELECT * FROM tblagency WHERE category = '".$c."'";
  75.  
  76. $result = mysql_query($sql);
  77. echo "<select name=\"agency\">";
  78. while($row = mysql_fetch_array($result))
  79.  {
  80.  
  81. echo "<option value=".$row['id'].">".$row['agency']."</option>";
  82.  
  83. }
  84. echo "</select>";
  85. mysql_close($con);
  86. ?>
  87.  
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!
May 12 '09 #1
2 3689
Ciary
247 Expert 100+
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
May 12 '09 #2
acoder
16,027 Expert Mod 8TB
First remove the current options:
Expand|Select|Wrap|Line Numbers
  1. selObj.options.length = 0;
then add the options (using a loop)
Expand|Select|Wrap|Line Numbers
  1. selObj.options[i] = new Option(text, value);
This means you will need to change the PHP code to echo appropriate output.
May 12 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by russ | last post: by
7 posts views Thread by Jeff Uchtman | last post: by
9 posts views Thread by Tarscher | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.