Current location: Hot Scripts Forums » General Web Coding » JavaScript » Dynamic select box from database values


Dynamic select box from database values

Reply
  #1 (permalink)  
Old 05-03-05, 07:46 AM
PrashantJsp PrashantJsp is offline
New Member
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Dynamic select box from database values

I have 4 select boxes named state, district, tehsil, village. By default on form load the state is popluated with all the states in database. After selecting a state, the form is refreshed and the district select box is populated with districts of the selected state. Similiarly for tehsils and blocks. This is time and database query consuming. I need to retrieve all the districts, tehsils, etc in one nX4 matrix and use javascript to appropriately populate the respective select boxes on change of the other. Can anybody send me javascript code for that, may be for just 2 levels, state and district? I can insert the jsp code in that.
Reply With Quote
  #2 (permalink)  
Old 05-03-05, 03:48 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Write out the database in this form:
Code:
<head>
<script>
States = {
 State1 : ['District1', 'District2', 'District3'],
 State2 : ['District4', 'District5', 'District6']
}
/*
This creates an object called States with the arrays States.State1 and States.State2 containing the respective districts. If you have to use a space in the state name, use an _ instead and we'll replace it when it's written to the page. (Do similar things to other special characters)
*/

function ShowDistricts(){
        var List = document.getElementById('district')
        while(List.options[0]){
                List.removeChild(List.options[0])
        }
        var Sel=document.getElementById('state')
        var selected_state=Sel.options[Sel.selectedIndex].value
        for(var i in States[selected_state]){
                var new_option = document.createElement('Option')
                new_option.value=States[selected_state][i]
                new_option.innerHTML=States[selected_state][i].replace("_"," ")
                List.appendChild(new_option)
        }
}
/*
This function should generate a new list of districts depending on which state is selected, it also clears the list before populating it with new names.
*/

</script>
</head>
<body>
<form>
<select id="state" onchange="ShowDistricts()">

</select>
<p>
<select id="district">

</select>
</form>
</body>
I haven't tested this code, but I hope you get the idea.
You can do it in other ways too by for example making each state and district representing an index in a matrix, but I decided to use an object since I already had code for that from another app.

Last edited by TwoD; 05-03-05 at 03:54 PM.
Reply With Quote
  #3 (permalink)  
Old 05-04-05, 12:29 AM
abraxxas's Avatar
abraxxas abraxxas is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

I tested it and it worked. Charming piece of code, I must say. Good job, TwoD!

The only thing I've noticed about it worth making a remark on is that the JavaScript replace function only replaces one occurance of the character, so there you might be better of with regEx or a loop with replacements.
__________________
Will work for bandwidth

Last edited by abraxxas; 05-04-05 at 01:29 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 08:34 PM
Forms help input box switch to Select box dstran JavaScript 1 04-19-05 12:17 AM
Dynamic Control Values khan_mansoor ASP.NET 1 11-14-04 11:28 PM
Using 'SELECT from' with value from a text box. steve711 PHP 1 06-17-04 06:37 PM
need help with retaining select list values sky4est PHP 1 03-25-04 05:00 PM


All times are GMT -5. The time now is 10:47 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.