Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic Dropdown Box

Member
 
Join Date: Feb 2007
Posts: 75
#1: May 12 '09
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!

Ciary's Avatar
Expert
 
Join Date: Apr 2009
Location: The outer ring of hell
Posts: 238
#2: May 12 '09

re: Dynamic Dropdown Box


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: May 12 '09

re: Dynamic Dropdown Box


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.
Reply


Similar JavaScript / Ajax / DHTML bytes