Click here to Skip to main content
15,881,516 members
Articles / Programming Languages / Javascript
Article

Creating a Dynamic Combo Box w/o Refreshing Page

Rate me:
Please Sign up or sign in to vote.
4.56/5 (30 votes)
30 Jul 20032 min read 176.2K   2.5K   29   14
..using IFRAME.

Introduction

Forms involving a dynamically-linked combo box have to waste a server-trip to fetch the values from the database to populate that combo box based on the value of a dependent combo box. Links to a number of approaches to handle dynamically-linked lists are listed in Charles Carol's excellent online book here. I discovered a cross-browser (well almost cross-browser ;-). I tested the code in IE 5, Netscape 6 & 7 and Mozilla 1.0 & it worked fine. IFRAME is not supported in NN 4.x and lower) method to populate a dynamically-linked List Box without refreshing the page, by using the IFRAME tag.

On selecting a country from the first combo box, the cities in that country, retrieved from the database are populated into the second combo. The second combo is actually present inside a frame-border-less IFRAME. A teeny-weeny JavaScript function allows the value of the combo box to be available to the outer form & be submitted.

The source code

The zipped source code file consists of 2 ASP files - where.asp & cities.asp & a MDB file City.mdb containing a table of countries & cities.

where.asp is the page that you have to load first, using IIS. It has a reference to the cities.asp page through the IFRAME.

Here's the teeny-weeny function in where.asp that enables the combo box present in the form in the IFRAME to be accessed by the parent form:

JavaScript
function getIframe()
{
//to access the element in the Form 
//contained in the page called by the IFrame, use
//parent.iframeName.document.iframeFormName.field.value
//here the IFrame element is assigned to the 
//hidden variable city in the parent Form

document.form1.city.value=parent.Iframe1.document.form2.elements[0].value;

}

The city element referred in the script above is a hidden tag.

When a country name is chosen from the combo box, the selected value is passed as a querystring to cities.asp.

HTML
<select 
   onchange='document.getElementById("Iframe1").src="cities.asp?country="+
   this.value'>

It refers to the following IFRAME defined in the same page.

HTML
<IFRAME id="Iframe1" name='Iframe1' 
  FRAMEBORDER=0 SCROLLING='no' WIDTH=120 HEIGHT=40></IFRAME>

Check this Microsoft link for an insight into the IFRAME tag. If you're impressed with what can be done with IFRAME, take a look at Searchlite v2. Also take a look at two other related articles:

Links

Feedback

Let me know if this article is a killer or a dud. Or whatever you felt.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
India India
'Anil' Radhakrishna is a seasoned architect who enjoys working with Microsoft tools & technologies. He blogs quite regularly about his little discoveries and technical experiments on his blog called Tech Tips, Tricks & Trivia. He loves building mash-ups using public Web APIs.

Comments and Discussions

 
Questioniframe Pin
jguemez22-Apr-11 6:15
jguemez22-Apr-11 6:15 
GeneralCreating a Dynamic Combo Box w/o Refreshing Page Pin
RayRit18-Nov-09 13:13
RayRit18-Nov-09 13:13 
QuestionCan you help me out by doing the same With PHP Pin
arunudu14-Dec-07 1:07
arunudu14-Dec-07 1:07 
Questioncan you upgrade this in asp.net Pin
pabidi27-Jun-07 1:33
pabidi27-Jun-07 1:33 
GeneralExpansion to 3 combo box Pin
geervanibs4-Jun-07 20:16
geervanibs4-Jun-07 20:16 
GeneralThanks 4 this Pin
Cristiano Shrestha22-May-07 19:06
Cristiano Shrestha22-May-07 19:06 
QuestionHow to expand the select box to be 3 or more select box??? Pin
neo_msi202-May-07 22:05
neo_msi202-May-07 22:05 
GeneralThanks so much! [modified] Pin
enfleshjsc29-Mar-07 5:05
enfleshjsc29-Mar-07 5:05 
QuestionHow do i extend it to three combo boxes Pin
Member 5196498-Aug-03 6:15
Member 5196498-Aug-03 6:15 
AnswerRe: How do i extend it to three combo boxes Pin
geervanibs4-Jun-07 20:25
geervanibs4-Jun-07 20:25 
GeneralYou have a creative solution but... Pin
kbuchan4-Aug-03 2:19
kbuchan4-Aug-03 2:19 
GeneralRe: You have a creative solution but... Pin
Richard Deeming4-Aug-03 22:56
mveRichard Deeming4-Aug-03 22:56 
GeneralJSP Pin
Juan Carlos Montenegro18-Dec-02 5:45
Juan Carlos Montenegro18-Dec-02 5:45 
Generalsometinh different Pin
tommy skaue12-Nov-02 0:18
tommy skaue12-Nov-02 0:18 
I did something entirely different... i wante do write two different combo's to a page and have the user change combo with a checkbox (ex. show limited list/show full list). I wrote two combos to the html thru a asp script where they both where wrapped with a <SPAN>-tag and one of them had
<SPAN style="display:'none'">
then i wrote a javascript to hide and display the combo's by the value of the checkbox...


------------------------------------------
Hey! Stop reading my signature... stop it!
hmm... you're still reading it...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.