Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 26th, 2003, 09:10 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamic dropdown boxes

hello,

the logic behind the code i post is as follows:

the user selects a studentid from the dropdown box and the second dropdown box automatically displays the corresponding student name.

i need to post the data in the dropdown boxes to another database. i am ok with that just that there's a wrong posting taking place.

the code posts the same drop down value to two fields - studentid field gets the studentid value and the studentname field also gets the studentid value. if you look well in the code you will see why it is doing that. i have not yet managed to find a solution to this probelm and so i am asking for help.

<%
Dim conn
Dim rs
Dim strconn

Session("DataConn_ConnectionString") = _
"DRIVER={Microsoft Access Driver (*.mdb)};uid=;pwd=letmein; DBQ=" & Server.MapPath("\db.mdb")
strconn = Session("DataConn_ConnectionString")
set conn = server.createobject("adodb.connection")
set rs = server.createobject("adodb.recordset")
conn.open strconn
strsql = "SELECT StudentId, StudentName FROM students"
rs.open strsql, conn, 2, 2

Response.Write "<SELECT name=studentid onchange=""studentname.value = this.value;"">"
While not rs.EOF
Response.Write "<OPTION value=" & rs("studentid") & ">" & rs("studentid") & "</OPTION>"
rs.MoveNext
Wend
Response.Write "</SELECT>"
rs.MoveFirst
Response.Write "<SELECT name=studentname onchange=""studentid.value = this.value;"">"
While not rs.EOF
Response.Write "<OPTION value=" & rs("studentid") & ">" & rs("studentname") & "</OPTION>"
rs.MoveNext
Wend
Response.Write "</SELECT>"
%>

is there a way whereby i could solve this problem? studentid dropdown box posts to studentid field and studentname posts to studentname field.

i would appreciate your help.

fmh002
 
Old June 26th, 2003, 08:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Firstly,
Is it imperative that you hold the student name in the second table. the whole point of relational database is to avoid that kind of thing. If you know the id you can find the name from the students table.
OR
Puting that aside you can populate the field straight from the database when you do your insert. Using the second dropdown for display only.
strsql = "Insert into newTable select studentID, studentName from students where studentId = " & request("studentID")
OR
If you really want to do it in code you use the selected index property of the dropdown boxes. Again set the value of the studentname dropdown to studentname instead of id. The onchange event will set the index value of the one box to the index value of the the otherbox
Take note of the new onchange events.
eg

Response.Write "<SELECT name=studentid onchange=""studentname.options(studentid.selectedI ndex).selected='true';"">"
While not rs.EOF
Response.Write "<OPTION value=" & rs("studentid") & ">" & rs("studentid") & "</OPTION>"
rs.MoveNext
Wend
Response.Write "</SELECT>"
rs.MoveFirst
Response.Write "<SELECT name=studentname onchange=""studentid.options(studentname.selectedI ndex).selected='true';"">"
While not rs.EOF
Response.Write "<OPTION value=" & rs("studentname") & ">" & rs("studentname") & "</OPTION>"
rs.MoveNext
Wend
Response.Write "</SELECT>"

This is possible because you are using the same recordset to poputale both boxes so the ids and names will be in the same order and have the same index.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Menus - Using multiple dropdown boxes Sebastiaan ASP.NET 1.0 and 1.1 Basics 1 April 18th, 2007 06:25 AM
Dynamic Menu - Using multiple dropdown boxes Sebastiaan Javascript How-To 2 December 1st, 2003 05:36 AM
Dynamic Menu - Using multiple dropdown boxes Sebastiaan Classic ASP Basics 3 June 7th, 2003 06:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.