473,371 Members | 1,448 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,371 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 3961
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.