Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 21st, 2006, 01:31 AM
Registered User
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

<div align="left">
hey frends..
i had to create a asp page with 2 select pulldowns(state,city),options to b displayed in the CITY depends on STATE choice.

the option list is to retrived from a db "dynamically".

i have a table with fields--state,stateID,city,cityID.

here is the code snippet i have..it works fine!! but....

<script language=javascript >
var States = new Array();
var Cities = new Array();

function addState(state) {

    v1 = States.length;
   States.length ++;
   States[v1] = state;
   v1 = Cities.length;
   Cities.length ++;
   Cities[state] = new Array();

function addCity(state,city) {
      v1 = Cities[state].length;

    Cities[state].length ++;

    Cities[state][v1] = city;

function loadStateList() {

    var ctrlState = document.frmAddress.State;
    ctrlState.options.length = 0;
    for (i=0;i<States.length;i++) {
    ctrlState.options[i] = new Option(States[i],States[i]);

function loadCityList() {

    var ctrlState = document.frmAddress.State;
    var selState = ctrlState.options[ctrlState.selectedIndex].value;

    var ctrlCity = document.frmAddress.City;
    ctrlCity.options.length = 0;

    for (i=0;i<Cities[selState].length;i++) {
        ctrlCity.options[i] = new Option(Cities[selState][i]);

<body onload="loadStateList();loadCityList();">

' Open Database and setup a recordset with States Listed.
set conn = server.createobject("ADODB.Connection")
Conn.open("driver={SQL Server};server=xxxxxxx;uid=xxxxxxx;pwd=xxxxxxx;database=xxxxxxx;network=dbmssocn")
strSQL = "Select State, city from tbAddress Order by State,city"
set rs = conn.execute(strSQL)

if rs.eof then
    response.write("No Addresses Found")
    set rs=nothing
end if

strState = ""
strCity = ""
do until rs.eof
    if not rs("State") = strState then
        response.write("<script>addState('" & rs("State") & "')</script>")
        response.write("<script>addCity('" & rs("State") & "','" & rs("City") & "')</script>")
        strState = rs("State")
        strCity = rs("City")
        if not rs("City") = strCity then
            response.write("<script>addCity('" & rs("State") & "','" & rs("City") & "')</script>")
            strCity = rs("City")
        end if
    end if
set rs=nothing

<form name="frmAddress" method="POST" action="">
            <td>STATE ID:</td>
            <td><input type=text hidden="true" name="text1" /></td>
            <td>CITY ID</td>
            <td><input type=text hidden="true" name="text2" /></td>
                <select size="1" name="State" onChange="loadCityList();">
                <select size="1" name="City">
    <input type="submit" value="Submit" name="save" />
    <input type="reset" value="Reset" name="clear" />
</div id="left">
NOW..how can i modify the code,so that wen the option is selected in the STATE & CITY lists,simulteneously the stateID,cityID are filled in 2 hidden textboxes,Text1 & Text2..

kindly help..

Reply With Quote
  #2 (permalink)  
Old December 21st, 2006, 05:56 PM
Friend of Wrox
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post

probably the best thing to do is to add a Javascript to run when the form is submitted that will populate the values for you prior to form submission.

function popFields()
    // get the values
    var state=document.frmAddress.State.options[document.frmAddress.State.selectedIndex].value;
    var city=document.frmAddress.City.options[document.frmAddress.City.selectedIndex].value;

    // populate the values

    // submit the form
    return true;

then simply change the FORM tag to the following :

<form name="frmAddress" method="POST" action="" onsubmit="return popFields()">

see http://www.htmlcodetutorial.com/form..._onSubmit.html for another example.
Reply With Quote
  #3 (permalink)  
Old December 21st, 2006, 08:44 PM
Registered User
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Hi..Thanx really..this idea dint strike 2me..
but tht doesn help 2 get wat i want..


In my code,only the STATE & CITY values are used/passed..HOW CAN I put some code that ll retrive the STATE_ID n CITY_ID values too,n place it in textboxes,Depending on the STATE & CITY choices respectively..

kindly help.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Code fix HemaRaj Javascript 5 January 31st, 2008 04:41 PM
Property dropdown box in C# code-behind in VS jayaraj123 C# 2005 1 April 5th, 2007 12:54 AM
Dynamic Dropdown bullsb Classic ASP Professional 4 March 9th, 2007 07:42 AM
dynamic dropdown boxes fmh002 Classic ASP Basics 1 June 26th, 2003 08:35 PM

All times are GMT -4. The time now is 03:30 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.