473,394 Members | 1,866 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,394 software developers and data experts.

Populate Child Dropdown without Page Refresh / Page Return From Database

76
I've argued with this for quite some time and I've seen a number of other people on various other sites asking for this same thing with no answer so I figured that now that I have solved it I would share it. It could probably use some tweaking but anyway feel free to use this code as you see fit.

The idea is that you have a dropdown, and dependent on what they pick on the first dropdown will determine what they get in the second one. Now the kicker I had is that both are populated by the database as well.. so here's what I've got.

The following assumptions are made.
The two tables are related in some fashion, they have an ID that can be associated with one another. For the sake of this example I'm calling it "MyID"

Also Running on the assumption that you already have a connection string and Conn is your connection.

Only tested to work in IE. If you're using Firefox you need to add a try/catch on the add element and add a different method of adding the element to the container.

Code goes before the BODY tag
Expand|Select|Wrap|Line Numbers
  1. <%Dim sql,rsFirst,rsSecond,x
  2. Set rsFirst = Server.Createobject("ADODB.recordset")
  3. sql = "SELECT * FROM FirstTable"
  4. rsFirst.Open sql,Conn
  5.  
  6. Set rsSecond = Server.Createobject("ADODB.recordset")
  7. sql = "SELECT * FROM SecondTable"
  8. rsSecond.Open sql,Conn
  9.  
  10. 'Write to page a script to declare a javascript array to hold your data.
  11. x=0
  12. %>
  13. <%="<script language=javascript>" & vbCrLf%>
  14. <%="var myArray = new Array()" & vbCrLf%>
  15. <%do until rsSecond.eof%>
  16. <%="myArray[" + x + "] = '" + rsSecond("MyID") + ";" + rsSecond("LabelField") + ";" + rsSecond("LastField") + "';"%>
  17. <%x=x+1%>
  18. <%rsSecond.movenext%>
  19. <%loop%>
  20. <%="</script>"%>
  21.  
  22. <script language="javascript">
  23.  function setField(setField,setID)
  24. {
  25.   var container = document.getElementById(setField);
  26.   container.length = 0;
  27.   var tempLine;
  28.  
  29.   for (var x=0;x<myArray.length;x++)
  30.   {
  31.     tempLine = myArray[x].split(";");
  32.     if (tempLine[0] == setID)
  33.     {
  34.       var newOpt document.createElement('option');
  35.       newOpt.text = tempLine[1];    //LabelField
  36.       newOpt.value = tempLine[0];   //MyID
  37.       container.add(newOpt);
  38.     }
  39.   }
  40. }
  41. </script>
  42.  
HTML Form
Expand|Select|Wrap|Line Numbers
  1. <label>Select First:</label>
  2. <select id="parentField" name="parentField" onChange="setField('childField',this.value)">
  3.   <%do until rsFirst.eof%>
  4.     <option value="<%=rsFirst("MyID")%>"><%=rsFirst("FirstLabel")%></option>
  5.   <%rsFirst.movenext%>
  6.   <%loop%>
  7. </select>
  8.  
  9. <label>Select Second:</label>
  10. <select id="childField" name="childField">
  11. </select>
  12.  
Oct 17 '06 #1
2 5810
Try using AJAX for this.
Oct 24 '06 #2
puafo
3
I am trying your code for "Populate Child Dropdown without Page Refresh / Page Return From Database"

I am hopeless at Javascript, and I have changed your script to the following and get an error...

Expand|Select|Wrap|Line Numbers
  1. <%
  2. Set rsFirst = Server.Createobject("ADODB.recordset") 
  3. sql = "SELECT categoryID, categoryActive, categoryTitle FROM category" 
  4. rsFirst.Open sql,adoCon 
  5.  
  6. Set rsSecond = Server.Createobject("ADODB.recordset") 
  7. sql = "SELECT categorySubID, categorySubActive, categorySubTitle, categorySubCategory FROM categorySub" 
  8. rsSecond.Open sql,adoCon 
  9.  
  10. x = 0
  11.  
  12. %>
Head
Expand|Select|Wrap|Line Numbers
  1. <script language=javascript> 
  2. var myArray = new Array() 
  3. <% do until rsSecond.eof %> 
  4. <%="myArray[" + x + "] = '" + rsSecond("categorySubID") + ";" + rsSecond("categorySubTitle") + ";" + rsSecond("categorySubCategory") + "';"%> 
  5. <% x=x+1 %> 
  6. <% rsSecond.movenext
  7. loop %> 
  8. </script>
  9. <script language="javascript"> 
  10.  function setField(setField,setID) 
  11.   var container = document.getElementById(setField); 
  12.   container.length = 0; 
  13.   var tempLine; 
  14.  
  15.   for (var x=0;x<myArray.length;x++) 
  16.   { 
  17.     tempLine = myArray[x].split(";"); 
  18.     if (tempLine[0] == setID) 
  19.     { 
  20.       var newOpt document.createElement('option'); 
  21.       newOpt.text = tempLine[1];    //LabelField 
  22.       newOpt.value = tempLine[0];   //MyID 
  23.       container.add(newOpt); 
  24.     } 
  25.   } 
  26. </script>
Body
Expand|Select|Wrap|Line Numbers
  1. <label>Select First:</label> 
  2. <select id="parentField" name="parentField" onChange="setField('childField',this.value)"> 
  3. <% do until rsFirst.eof %> 
  4. <option value="<%=rsFirst("categoryID")%>"><%=rsFirst("categoryTitle")%></option> 
  5. <% rsFirst.movenext
  6. loop %> 
  7. </select> 
  8. <label>Select Second:</label> 
  9. <select id="childField" name="childField"> 
  10. </select>
the error is on line 21
Expand|Select|Wrap|Line Numbers
  1. <%="myArray[" + x + "] = '" + rsSecond("categorySubID") + ";" + rsSecond("categorySubTitle") + ";" + rsSecond("categorySubCategory") + "';"%>
the error is
Expand|Select|Wrap|Line Numbers
  1. Type mismatch '[string: &quot;myarray[&quot;]
any help would be greatly appreciated. thanks in advance.
Aug 11 '09 #3

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

Similar topics

1
by: Fresh Air Rider | last post by:
Hi Everyone Scenario A Webage has two dropdown lists, one for Suppliers and one for Products. When a user selects a supplier, it then populates the Products dropdown list with the relevant...
3
by: Matt | last post by:
Is there a way to execute server code without doing a complete page refresh in .Net? I have an ASP page to convert that uses XMLHTTP and I'm wondering if I should continue to use it in ASPX or do...
9
by: PK9 | last post by:
I'm having an issue with the "Refresh" of an asp.net page. The refresh is actually calling my last onClick event. I thought that asp.net was supposed to be stateless in that it shouldn't...
10
by: phforum | last post by:
Hi, I wrote a PHP page for user input the information to search the database. And the database data will update every second. I want to set the auto refresh to get the data from database every...
11
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
9
by: preetksaini | last post by:
hi i am having a page in which i have 3 dropdowns,values of 2nd dropdown comes based on 1st and values of 3rd dropdown come based on 2nd.using AJAX+PHP to load dropdowns. but whenever i refresh...
1
by: fran7 | last post by:
Hi, Anyone know how to populate a dropdown list with data from database. I have an interface that generates the following code for names in my database. ..asp <%If Trim(rsCard("Author"))<>""...
2
by: Hrvoje Vrbanc | last post by:
Hello! I have an ASP.NET page that displays some data from an SQL Server 2005 database, using a Select query. The database is updated by another piece of software, independently of the ASP.NET...
11
by: tokcy | last post by:
Hi everyone, I am new in php and ajax, i am facing the prob while i click on element of first drop down then in second dropdown all element showl come from database. I mean i have three dropdown 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
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
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...

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.