473,915 Members | 5,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Dropdown Box

107 New Member
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 4024
Ciary
247 Recognized Expert New Member
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 Recognized Expert Moderator MVP
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
3413
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 Private Sub txtFindComposer_Change() Requery Me!.SetFocus
1
7600
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 dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
1
4846
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 the table contains a SELECT box populated on the initial load. Every time I postback I'm inserting a column into the table, the dropdown always remains in the last column. First time I postback the dropdown is populated okay. The second time...
7
2376
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 matter tonight. Thank Jeff
0
2925
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 strDataValueField; string strDataTextField; public CreateEditItemTemplateDDL(string DDLName,string DataValueField,string
9
3638
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 to create the dynamic controls in the OnInit stage of the lifecycle. Since data from static controls is not yet available in the OnInit stage I can't know what dynamic control I have to create.
1
1933
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 consists of departments- It has 3 options All issues, dept A, dept B If dept A is chosen in the first dropdown, the second dropdown shows all members in dept A and all issues are shown for dept A are shown below. Now if i choose a member in second...
1
4673
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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by pressing Remove button the selecetd row will be removed. I used viewstate to keep my value for postback, I want by changing selectedvalue of...
0
3513
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- 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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9881
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10542
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8100
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.