Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Populating a combo based on another combo

    I want to populate a combo box that depends on the selection of another combo box.
    for example, from the first combo i select any country name, the second combo automatically fills the city names for that country from the database.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Do you want the page not to refresh when a selection is made? If yes, then you need a javascript solution, otherwise, you need an ASP solution. If you want the former, you can find several scripts out there. But even then, you still need ASP to generate all the options. Just google for "double combo javascript".
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes i want the to refresh. I've wrote the following code but the problem is that the first combo always returns the first value which is also at the top in the DB. How it can be keep tracked that when pages refreshes it holds the selected value.
    <html>
    <head>
    <title>Untitled Document</title>
    <script>
    function doSubmit()
    {
    document.fSelect.action = "combo.asp"
    document.fSelect.method ="post"
    document.fSelect.submit();
    }
    </script>
    </head>

    <body>
    <%
    set con = Server.CreateObject("ADODB.Connection")
    set Rs = Server.CreateObject("ADODB.Recordset")

    Con.Open strConnect
    'Rs.Open "CustomerInfo", Con, 1, 2

    SQL = "Select * FROM City"
    Rs.Open SQL, Con, 1, 2
    %>
    <form name="fSelect" method="post" action="comboResult.asp">
    <p> city
    <select name="City" id="city" onchange="doSubmit()">

    <% While Not Rs.EOF %>
    <option value= <% = Rs.Fields("SNo")%>><% =Rs.Fields("City")%></option>
    <%

    Rs.MoveNext
    Wend

    %>
    </select>
    </p>
    <%
    'response.write (Session.value("val"))
    If Request.Form("City") > 0 Then

    'strSQl= "select * from Towns where SNo=" & Request.Form("Town")
    strSQl= "select * from Towns where SNo=SNo"
    Rs.close
    Rs.open strsql, con

    End If

    %>

    <p>town
    <select name="town" id="town">
    <% If Rs.RecordCount > 0 Then
    While Not Rs.EOF %>
    <option value=<% =Rs.Fields("Towns")%>><% =Rs.Fields("Towns")%></option>
    <% Rs.Movenext
    Wend
    End if
    %>
    </select>
    </p>
    <p>&nbsp;</p>
    <input type=submit>
    </form>
    </body>
    </html>

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <script>
    function doSubmit(f)
    {
      f.action = "combo.asp";
      f.method = "post";
      f.submit();
    }
    </script>
    </head>
    
    <body>
    <%
    set con = Server.CreateObject("ADODB.Connection")
    set Rs = Server.CreateObject("ADODB.Recordset")
    
    Con.Open strConnect
    'Rs.Open "CustomerInfo", Con, 1, 2
    
    SQL = "Select * FROM City"
    Rs.Open SQL, Con, 1, 2
    
    dim selectedCity, rsNo, selected
    selectedCity = request.form("City")
    %>
    <form name="fSelect" method="post" action="comboResult.asp">
    <p> city
    <select name="City" id="city" onchange="doSubmit(this.form)">
    
    <%
    While Not Rs.EOF 
      rsNo = Rs.Fields("SNo")
      if selectedCity = rsNo then selected = " selected=""selected""" else selected=""
    %>
    <option value="<% = rsNo%>"<% = selected%>><% =Rs.Fields("City")%></option>
    <%
      Rs.MoveNext
      Wend
    %>
    </select>
    ...
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its not working ... the problem is same, page refreshes after the selection but again shows the first value of the DB.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Do you have the page online?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, i don't hav the page online, i'm using it at local host.
    I have pasted the code above but if u need i'll upload it.
    Can i have you messenger id so can talk u directly?

  • #8
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    At some point you will need a JavaScript to actually create the inter-dependant selects. For that check out Chained Selects: http://www.dynamicdrive.com/dynamici...hainedselects/
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by fidodido
    No, i don't hav the page online, i'm using it at local host.
    I have pasted the code above but if u need i'll upload it.
    Can i have you messenger id so can talk u directly?
    Can you post the generated HTML code for the combobox?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #10
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <!-- #include file=../db/Connect.asp -->

    <html>
    <head>
    <title>Untitled Document</title>

    <script>
    function doSubmit()
    {
    document.fSelect.action = "comb.asp"
    document.fSelect.method ="post"
    document.fSelect.submit();
    }
    </script>

    </head>

    <body>
    <%
    set con = Server.CreateObject("ADODB.Connection")
    set Rs = Server.CreateObject("ADODB.Recordset")

    Con.Open strConnect

    SQL = "Select * FROM City"
    Rs.Open SQL, Con, 1, 2
    %>
    <form name="fSelect">
    <p> city
    <select name="City" id="city" onchange="doSubmit()">

    <% While Not Rs.EOF %>
    <option value= <% = Rs.Fields("SNo")%>><% =Rs.Fields("City")%></option>
    <%

    Rs.MoveNext
    Wend

    %>
    </select>

    <P>
    <input type=text name=focus value="<%= Request.Form("City") %>">
    <%

    If Request.Form("City") > 0 Then
    Rs.close
    strSQl= "select * from Towns where SNo=" & Request.Form("focus")
    'strSQl= "select * from Towns where SNo=SNo"

    Rs.open strSQL, con

    End If

    %>

    <p>town
    <select name="town" id="town">
    <% If Rs.RecordCount > 0 Then
    While Not Rs.EOF %>
    <option value=<% =Rs.Fields("Towns")%>><% =Rs.Fields("Towns")%></option>
    <% Rs.Movenext
    Wend
    End if
    %>
    </select>

    </form>
    </body>
    </html>

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You did not incorporate the changes I made.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, the above code is pasted wrong. But your code is doing the same.
    Select any city name from the list, then page refreshes and shows the first value which is also first in the DB. It didn't stores the user's selected value.
    Can i send u the file so that u may view it correctly.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Is the page submitting to itself when onchange occurs in the combo box?
    You must specify the current page in the action attribute.
    Code:
    function doSubmit(f)
    {
      f.action = "combo.asp"; //must be the current page
      f.method = "post";
      f.submit();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    New Coder
    Join Date
    Jan 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes its written correctly, i've checked it. Page refreshes after the selection from first combo but didn't stores the selected value and always shows the first value.

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    It's hard if we can't see the page in action.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •