473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 3967
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

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

Similar topics

9
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: russ | last post by:
Hi all, Here's a problem I'm having with a dynamic table. Following the guidelines here (http://www.codeproject.com/aspnet/dynamiccontrols.asp), which make perfect sense. The problem is that...
7
by: Jeff Uchtman | last post by:
I know I have done this but my mind is fried. I have a dynamic dropdown in a form. I need to pull both the dynamic dropdown's ID and name listed in the dropdown. Need a little help with grey...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
1
by: phpnewbie2007 | last post by:
I have 2 dynamic PHP dropdowns: The second dropdown populates from the first, depending on what is selected in the first. The page is showing issues in a department: The first dropdown...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.