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 28
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    populate dropdown box from DB using ASP

    Hi guys, I'am a newbee in ASP i have a problem selecting a value from the dropdown box which i papulated from database.
    I have a select form name style then this is the select statement Select distinct proptype from developerproperties order by proptype and the result is
    Garden Home
    Golden Mile
    Shoreline
    Signature Villa
    Town Home

    Now if i just select the Shoreline from the dropdown box there is another dropdown which only relevant to the shoreline for example
    if shoreline selected the result of the next dropdown should be only TypeA,TypeB,TypeC,TypeD,TypeE,TypeF

    how can i do this on asp or is there any code like jsp on these?please help.

    Many Thanks

    Jerus

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Posts
    122
    Thanks
    1
    Thanked 0 Times in 0 Posts

  • #3
    dev
    dev is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u can write a javascript for the event onchange in the first combo.pass a action variable like "action=loadcombo2" also pass the value of the first combo.

    within the page get the querystring of action variable and check for the condition
    if action = "loadcombo2" then
    perform the loading of combo2.
    end if

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Dev and tcadieux, but my first dropdown box is database driven, if i will select Shoreline it will only show the relevant data for the second dropdown.

    According to tcadieux the script is good but the data is hardcoded, how can i create these javascript if the data are from the database, izt possible?

    Many THanks

    Jerus

  • #5
    dev
    dev is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its somethng like this.please check the below code

    <html>
    <head>
    <script language="javascript">
    function fngetvalue()
    {
    var strvalue;
    strvalue=<get the value of the combo>; window.document.<formname>.action="<pagename>.asp?value="+strvalue+"&action=loadcombo2"
    }
    </script>
    </head>
    <body>
    combo1
    ======
    <select id="cb1" onchange="fngetvalue()">
    <option value="1">Garden Home</option>
    <option value="2">Golden Mile</option>
    <option value="3">Shoreline</option>
    <option value="4">Signature Villa</option>
    <select>

    <%
    'loading second combo
    dim action,Value
    action=request.querystring("action")
    if action ="loadcombo2" then

    Value=request.querystring("value")//value of the first combo

    'write the select query
    strselqry = "select * from tblname where Proptype="& Value &""
    'Execute the query
    End if
    %>



    </body>
    </html>

  • #6
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Dev, these is gonna be a big help, i will try to integrate the codes you've given. Thanks alot. Jerus

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Dev,

    Im still not getting the result. I will show you the entire part of my code, please bear with me. I dont get the second dropdown condition and also the javascript.

    this is the code.

    <%@ Language = VBscript %>
    <html>
    <head>
    <title>Database Search</title>
    <script language="javascript">
    function fngetvalue()
    {
    var strvalue;
    strvalue=<get the value of the combo>;
    window.document.propsearch.action="dropdowndb.asp?value="+strvalue+"&action=loadcombo2"
    }
    </script>
    </head><body>

    <%
    Dim objconn,objRS,strSQL,style
    style=Request.QueryString("style")

    Set objconn = Server.CreateObject("ADODB.Connection")
    objconn.ConnectionString = "Provider=sqloledb;"//i just rid the whole connectionstring
    objconn.Open
    Set objRs = Server.CreateObject("ADODB.Recordset")

    '''''First drop down list starts here'''''

    strSQL = "Select distinct proptype from developerproperties order by proptype"

    objRS.Open strSQL, objconn

    Response.Write "<form method=post name='propsearch' action='jrs_results.asp'><select name=style onchange='fngetvalue()'>"
    Response.Write "<option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("proptype") & "'>" & objRS("proptype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"
    Response.Write "<br><br>"

    ''' Second drop down list starts here ''''
    dim action,Value
    action=request.querystring("action")
    if action ="loadcombo2" then
    Value=request.querystring("value")'value of the first combo
    strSQL = "Select distinct propertyplantype from developerproperties where proptype='" & style &"' order by propertyplantype"
    else
    strSQL = "Select distinct propertyplantype from developerproperties order by propertyplantype"
    end if

    objRS.Open strSQL, objconn

    Response.Write "<select name=ptype><option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("propertyplantype") & "'>" & objRS("propertyplantype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"
    Response.Write "</form>"
    objconn.Close
    %>
    </body>
    </html>

  • #8
    dev
    dev is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    did u try putting alert messages inside the javascript (just for checking purpose).so that u can get to know the value of first combo is getting passed.

    one more thing is the second combo in the same page or the different page.

  • #9
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    strvalue=<get the value of the combo>;
    I think that dev intended you to replace this with some javascript that gets the value of the combo? This leads us to a problem:

    Code:
    <select name=style onchange='fngetvalue()'>
    "style" is a HTML attribute. I'd be very careful about calling HTML elements "style". Especially if it doesn't have quotes around it. Lets's call it "selStyle" instead, and give it an ID.

    Code:
    <select name='selStyle' id='selStyle' onchange='fngetvalue()'>
    Then you can update your line of javascript so that it works:

    Code:
    strvalue = document.getElementById('selStyle').options[document.getElementById('selStyle').selectedIndex].value;
    So: when your first combo box is changed (ie: something is selected), a javascript function is fired. This gets the value of the selected option, and submits the form to dropdowndb.asp (ie: itself), sending the value of whatever was chosen in a variable called "value", and the string "loadcombo2" in a variable called "action". With it up to now?

    Now... the ASP is set up so that it looks to see if those variables are present:

    Code:
    action=request.querystring("action")
    if action ="loadcombo2" then
    value=request.querystring("value")
    And if they are, it fires a second request to the database, to populate the second dropdown. And here we find another problem:

    Code:
    where proptype='" & style &"'
    You populate this variable, "style", right near the top of the page:

    Code:
    style=Request.QueryString("style")
    But style isn't passed in the QueryString. The only variables that might be in the QueryString are "action" and "value". If you've changed the name of your combo box like I said, you'll be able to get it by going:

    Code:
    style = Request.Form("selStyle")
    but if you've been paying attention you'll know that this value will be exactly the same as the value you got in the now-confusingly-named QueryString variable "Value". So you may as well stop mucking about with the form and use the value that you've already got:

    Code:
    where proptype='" & value &"'
    Hope that helps....

  • #10
    dev
    dev is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi spudhead ,
    i have not asked the user to replace ths wth javascript .
    in the first sample what i have provided i have clearly given the ID to the 1st combo.
    but i have not shown the way to retrieve the combo's value.
    instead i gave strvalue=<get the value of the combo>;
    regards
    Dev

  • #11
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi dev and Spudhead,

    These form is only in one page, its a search page.so when i select the first dropdown it should be remain and the second dropdown will be the relevant to each value selected.

    These is my first dropdown:

    strSQL = "Select distinct proptype from developerproperties order by proptype"

    objRS.Open strSQL, objconn

    Response.Write "<form method=post name='propsearch' action='jrs_results.asp'><select name=propstyle onchange='fngetvalue()'>"
    Response.Write "<option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("proptype") & "'>" & objRS("proptype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"

    Results:Golden Mile, Garden Home, Shoreline, Town Home


    And the second dropdown:

    Dim style
    style = request.form(propstyle)

    if style = "Shoreline" then //lets say i selected these value from the first dropdown
    strSQL = "Select distinct propertyplantype from developerproperties where proptype='" & style &"' order by propertyplantype"
    else
    strSQL = "Select distinct propertyplantype from developerproperties order by propertyplantype"
    end if

    objRS.Open strSQL, objconn

    Response.Write "<select name=ptype><option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("propertyplantype") & "'>" & objRS("propertyplantype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"

    dev: from the javascript i dont get the loadcombo2 value, is that the id or name of the second dropdown?

    Spudhead: Thanks for the tips and clarifications.It is truely greatfull.

    Guys please help me on these.I cant get the problem.

    Regards,

    Jerus

  • #12
    dev
    dev is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u have to use the id of combo1 inorder to get the value.

  • #13
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Iam sorry Dev, im failed to implement the codes.

  • #14
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    You failed to implement the codes?



    This will not do, jerusking. You have failed in your task. You've let yourself down, you've let the forum down, you've let the whole internet down.



    Dude, post your whole page. You're so close it's almost painful.

  • #15
    New Coder
    Join Date
    Jan 2007
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Spudhead, these is the whole part of my code, bear with me trying to work these out but im failed.

    <%@ Language = VBscript %>
    <html>
    <head>
    <title>Database Search</title>
    <script language="javascript">
    function fngetvalue()
    {
    var strvalue;
    strvalue = <get the value of the combo>; // i have no idea about these line
    window.document.propsearch.action="dropdownbox01.asp?value="+strvalue+"&action=loadcombo2" // and also loadcombo2 value
    }
    </script>
    </head><body>

    <%
    Dim objconn,objRS,strSQL

    Set objconn = Server.CreateObject("ADODB.Connection")
    objconn.ConnectionString = "Provider=sqloledb;"
    objconn.Open
    Set objRs = Server.CreateObject("ADODB.Recordset")

    '''''First drop down list starts here'''''

    strSQL = "Select distinct proptype from developerproperties order by proptype"
    objRS.Open strSQL, objconn
    Response.Write "<form method=post nam='propsearch' action='jrs_results.asp'><select name='propstyle' id='propstyle' onchange=""fngetvalue()"">"
    Response.Write "<option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("proptype") & "'>" & objRS("proptype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"
    Response.Write "<br><br>"

    ''' Second drop down list starts here ''''
    Dim action, Value
    action=request.querystring("action")

    if action ="loadcombo2" then
    Value=request.querystring("value")
    strSQL = "Select distinct propertyplantype from developerproperties where proptype='" & Value &"' order by propertyplantype"
    else
    strSQL = "Select distinct propertyplantype from developerproperties order by propertyplantype"
    end if

    objRS.Open strSQL, objconn

    Response.Write "<select name='ptype' id='ptype'><option value='' SELECTED>No preferences</option>"
    Do While Not objRS.EOF
    Response.Write "<option value='" & objRS("propertyplantype") & "'>" & objRS("propertyplantype") & "</option>"
    objRS.MoveNext
    Loop
    objRs.Close
    Response.Write "</select>"
    Response.Write "</form>"
    objconn.Close
    %>
    </body>
    </html>


  •  
    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
    •