asp drop down list based on table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deerick
    New Member
    • Mar 2008
    • 1

    asp drop down list based on table

    Hi there I want to know how to add a drop down list based on value from a table in an asp form. Then I want the user to be able to submit the form, and the drop down list value that they have selected to be stored in a different table.

    Does anyone know how to do this, I present I am using the code below, which does create the drop down list, but then I not sure how to add its value to another table in my database ( Acesss)

    If anyone can help I would be very grateful!!!!

    <%@ Language=VBScri pt %>
    <%Option explicit
    Dim oRs, conn, connect, strSQL

    set conn=server.Cre ateObject ("adodb.connect ion")
    connect = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & Server.MapPath( "courses.md b") & ";Persist Security Info=False"
    conn.Open connect

    %>
    <html>
    <head>
    <title>Exampl e combo box</title>

    <script language="javas cript">
    <!--

    function dept_onchange(f rmSelect) {
    frmSelect.submi t();
    }

    //-->
    </script>
    </head>
    <body>
    The following was selected : <%=Request.Fo rm ("courses")% >

    <form name="frmSelect " method="Post" action="select. asp">
    <SELECT name=courses LANGUAGE=javasc ript onChange="retur n dept_onchange(f rmSelect)">
    <%
    Set oRs=Server.Crea teObject("adodb .recordset")
    strSQL = "SELECT DISTINCT CourseName FROM tblCourses ORDER BY CourseName"
    oRs.Open strSQL, conn

    Do while not oRs.EOF
    if Request.Form("c ourses") = oRs("CourseName ") then 'if this is the selected one then display as selected
    Response.Write "<OPTION VALUE = '" & oRS ("CourseName ") & "' SELECTED>"
    Response.Write oRs("CourseName ") & "</Option>"
    oRs.MoveNext
    else
    Response.Write "<OPTION VALUE = '" & oRs ("CourseName ") & "'>"
    Response.Write oRs("CourseName ") & "</Option>"
    oRs.MoveNext
    end if
    loop
    %>
    </SELECT>
    </form>

    </body>
    </html>
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi deerick,

    You need to write a bit of script to INSERT your data in to the other table. Once you've written that you need it to run when your form is submitted. The easiest way to do this is to change your forms action property to a different page, say InsertData.asp, then re-direct back to your original page once it has done it's work on the database. I've written a quick example below for you to take a look at.

    In your first page (select.asp) set the action of your form:
    Code:
    <form name="frmSelect" method="Post" action="InsertData.asp">
    When the drop down is selected the form will submit and go to the following page:

    InsertData.asp:

    Code:
     <% 
    Dim conn, connect, strSQL
     
    set conn=server.CreateObject ("adodb.connection")
    connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("courses.mdb") & ";Persist Security Info=False"
    conn.Open connect
     
    strSQL= " INSERT INTO table (Column1) VALUES ('" & Request.Form("courses") & "') "
     
    conn.Execute strSQL
     
    Response.Redirect("select.asp")
    %>
    Does this help at all? Let me know how you get on.

    Dr B

    Comment

    Working...