By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,049 Members | 723 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,049 IT Pros & Developers. It's quick & easy.

asp drop down list based on table

P: 1
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=VBScript %>
<%Option explicit
Dim oRs, 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

<title>Example combo box</title>

<script language="javascript">

function dept_onchange(frmSelect) {

The following was selected : <%=Request.Form ("courses")%>

<form name="frmSelect" method="Post" action="select.asp">
<SELECT name=courses LANGUAGE=javascript onChange="return dept_onchange(frmSelect)">
Set oRs=Server.CreateObject("adodb.recordset")
strSQL = "SELECT DISTINCT CourseName FROM tblCourses ORDER BY CourseName"
oRs.Open strSQL, conn

Do while not oRs.EOF
if Request.Form("courses") = 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>"
Response.Write "<OPTION VALUE = '" & oRs ("CourseName") & "'>"
Response.Write oRs("CourseName") & "</Option>"
end if

Mar 21 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 979
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:
Expand|Select|Wrap|Line Numbers
  1. <form name="frmSelect" method="Post" action="InsertData.asp">
When the drop down is selected the form will submit and go to the following page:


Expand|Select|Wrap|Line Numbers
  1.  <% 
  2. Dim conn, connect, strSQL
  4. set conn=server.CreateObject ("adodb.connection")
  5. connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("courses.mdb") & ";Persist Security Info=False"
  6. conn.Open connect
  8. strSQL= " INSERT INTO table (Column1) VALUES ('" & Request.Form("courses") & "') "
  10. conn.Execute strSQL
  12. Response.Redirect("select.asp")
  13. %>
Does this help at all? Let me know how you get on.

Dr B
Mar 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.