Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Oct 2003
    Location
    Barcelona
    Posts
    20
    Member #
    3732
    I use Dreamweaver MX, ASP JavaScript and SQL Server.


    I have two lists-menu objects in my page and I want that when de first list change the second list shows the values according the first list. The operation requires querying the database, but if it were possible I wish to do it with Arrays in JavaScript, maybe the problem would be the number of values (8.500 cities).

    In my database I have a view with four fields (State_name, State_id, City_name and City_id), in the first list-menu of the application there are all ‘State_id’s’ and the second list-menu will have only ‘Citi_id’s’ for the ‘State_id’ sent by the first list-menu.
    Now I achieved successfully the first list-menu with all States with a connection to database. But I don’ t know how can I achieve the second list according to the first when the first list change.

    Suggestions?

    Thanks,
    Cesar

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Use Javascript with a modal dialog popup to retrieve an array of values.

    Do something like this...

    address_form.asp:
    Code:
    <script language="javascript">
    <!--//
    function lookupCities() {
        var states = document.forms['address'].item['state'];
        var cities = document.forms['address'].item['city'];
    
        // Clear Cities
        for (var city in cities.options) {
            cities.remove(city);
        }
        
        // Get cities for that state
        var cityList = window.showModalDialog('city_lookup.asp?state_id=' 
            + states.options[states.selectedIndex].value, '', 
            'top:0px; left:0px; dialogWidth:0px; dialogHeight:0px;');
    
        // Add cities if any found for that state
        if (cityList) {
            for (var i = 0; i < cityList.length; i++) {
                var city = new Option(cityList[i][1], cityList[i][0]);
                cities.add(city);
            }
            cities.focus();
        } else {
            window.alert('No cities found');
        }
    }
    //-->
    </script>
    
    <form name="address">
    <select name="state" onchange="lookupCities();">
    </select>
    <select name="city">
    </form>
    </select>
    city_lookup.asp:
    Code:
    <%@Language=VBScript%>
    <%
        Dim oCn
        Set oCn = Server.CreateObject("ADODB.Connection")
        Dim oRs
        Set oRs = Server.CreateObject("ADODB.Recordset")
        Dim sql
        sql = "SELECT id, name FROM cities WHERE state_id = ?STATE"
        sql = Replace(sql, "?STATE", Request("state_id"))
    
        oCn.Open "CONNECTION_STRING"
        oRs.Open sql, oCn, 2, , 1
    
        Dim sReturn
        If oRs.EOF Then
            sReturn = "null"
        Else
            sReturn = "["
            Do While Not oRs.EOF
                sReturn = sReturn & "[" & oRs("id") & ", '" & oRs("name") & "']"
                oRs.MoveNext()
                If Not oRs.EOF Then sReturn = sReturn & ","
            Loop
            sReturn = sReturn & "]"
        End If
    %>
    <html>
        <head>
            <script language="javascript">
                <!--//
                window.returnValue = <%=sReturn%>;
                window.close();
                //-->
            </script>
        </head>
    </html>
    I don't really have time to explain it all, but basically, when you change the state, it pops up a modal dialog that loads city values into a javascript array from the database and returns them to the calling script, then closing the window and populating the cities list from the array.

    EDIT - P.S. - I haven't tested the above script, so you may encounter bugs in it. If you do, please fix them and repost the final script so that others may benefit from it too !!

  4. #3
    Junior Member
    Join Date
    Oct 2003
    Location
    Barcelona
    Posts
    20
    Member #
    3732
    Thank you, I' m going to see with more detail and I will tell you something.

    Cesar

  5. #4
    Junior Member
    Join Date
    Oct 2003
    Location
    Barcelona
    Posts
    20
    Member #
    3732
    This code is too difficult for me yet, anyway I will try to decipher with the aid of my manual.


    Thank you for your attention,
    Cesar


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:27 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com