473,698 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populate Child Dropdown without Page Refresh / Page Return From Database

76 New Member
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 5836
rarebond
1 New Member
Try using AJAX for this.
Oct 24 '06 #2
puafo
3 New Member
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
1256
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 products for the selected supplier.
3
2444
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 something else. The XMLHTTP is used in ASP to populate drop down boxes based on user input without having to send the entire page contents back and forth to the server. Our remote users have slow connections so it really helps to keep the amount...
9
7763
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 remember that I clicked a button before the refresh. Here is what is going on: 1) Page Loads 2) User enters some values, clicks the "Save" button 3) The onClick event handler for the save button saves the data to the database and the page is...
10
32875
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 minute. But the page always display the dialog box ask me to resend the information. How to disable this warning message. I using POST and REQUEST to get the data from user input page. Thanks all
11
7388
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 the Dropdown doesnt have any items.. The requirement is that as soon as one goes on typing the letters in the StudentName-textbox the corresponding matching names have to appear
9
5989
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 the page the selection and values of dropdown are lost. can anyone help me how to maintain the selected values of dropdown on page refresh (to maintain webpage state). thnx.
1
1868
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"))<>"" Then%> <%=rsCard("Author")%><%End If%> .. How does one populate a dropdown list??
2
2196
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 page. The ASP.NET page only displays data and it has nothing to do with inserting or updating data. What is the best way to force the refresh of the ASP.NET page every time a new record is inserted in the table?
11
4632
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. category which comes from database 2. brand which comes from databse according to content of first dropdown . and 3. price which is static. when i am doing these things without ajax on every onChange() thw whole page is refreshing that i do not...
0
8680
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
8609
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
9030
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...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
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
5861
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
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
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.