Link to home
Start Free TrialLog in
Avatar of LSE_IT_Training
LSE_IT_Training

asked on

ASP: Automatically select value in drop down list based on query string

We currently have a booking form for IT Training courses. The form pull course information from a database and populates the drop down list with course titles, times, etc.

We would like to increase the functionality of the form so that when a course is selected from the drop down list, the course prerequisites are displayed beneath the drop down list. We also want the selected option in the drop down list to remain selected

Using the code below, i can get the prerequisites text to appear below the drop down but can't get the selected option to remain selected in the drop down. When the page reloads, the drop down reverts to the first option ('Please select a course...' option)

I have tried to do this by comparing the value in the query string to the value on the drop down as the drop down list is generated, marking the option that matches as 'selected' but this isn't working.

How can I get the drop down to retain the selected option when the page reloads?

Thanks!

<%@LANGUAGE="VBSCRIPT"%>
<%
Option Explicit

'create database connection object
dim ObjConn
set ObjConn = Server.CreateObject("ADODB.Connection")
ObjConn.Open "DSN=students"

session.lcid=2057

'retrieve value of querystring in url
dim strURLID  
strURLID = Request.QueryString("CourseID")
      if IsEmpty(strURLID) then
            strURLID = "0"
      end if

'create recordset to hold upcoming course details for form drop-down
dim rsCourses
Set rsCourses = Server.CreateObject("ADODB.recordset")
rsCourses.ActiveConnection = ObjConn
rsCourses.open "SELECT [TESTCourse details].CourseID, [TESTCourse details].CourseTitleID, [Courses Title].CourseTitle, " &_
"[TESTCourse details].CourseDate, [TESTCourse details].CourseTime " &_
"FROM [Courses Title] INNER JOIN [TESTCourse details] ON [Courses Title].CourseTitleID = [TESTCourse details].CourseTitleID " &_
"WHERE (([TESTCourse details].CourseDate)>Now()) " &_
"ORDER BY [TESTCourse details].CourseDate;"



'create recordset to hold course prereq details

dim rsPrereq
Set rsPrereq = Server.CreateObject("ADODB.recordset")
rsPrereq.ActiveConnection = ObjConn
rsPrereq.open "SELECT [TESTCourse details].CourseTitleID, [TESTCourse details].CourseID, [TESTCourses Title].RemoveCourse, [TESTCourses Title].CourseRequirements" &_
" FROM [TESTCourse details] INNER JOIN [TESTCourses Title] ON [TESTCourse details].CourseTitleID = [TESTCourses Title].CourseTitleID" &_
" WHERE ((([TESTCourses Title].RemoveCourse)=0));"

rsPrereq.Filter = "CourseID ='" & strURLID & "'"
%>

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Content-Language" content="en-gb">
<meta name="Description" content="Booking Form for Student Training Tutorials">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="Keywords" content="Training, IT Training, Student Training, Booking Form">
<meta name="ProgId" content="FrontPage.Editor.Document">
<TITLE>IT Training: Student Training Tutorials Booking Form</TITLE>
<link rel="stylesheet" type="text/css" href="../../css/itservices.css">


<script language="javascript">

function checkWholeForm() {

---javascript form validation here---

</script>
</HEAD>

<BODY BACKGROUND="../images/images/its_tile.gif" BGCOLOR="#4A0194" TEXT="#000000" link="#4A0194" vlink="#808080" alink="#808080" topmargin="0" style="font-family: Arial; font-size: 10pt">

----html page header info here---
                 
                 
                 
<form method="POST" action="bookingConfirmationTEST.asp"   name="StudentBookingForm" id="StudentBookingForm">
   
      <p><b><font size="2">1. Which course do you want to book a place on?&nbsp;</font><nobr><font size="2">
      <br>
      <br>
      </font>
                  </nobr></b><nobr><font size="2">
      </font><select NAME="CourseID" SIZE="1" onchange="document.location='bookingTEST.asp?CourseID='+this.options[this.selectedIndex].value;">
      
      <option  value="0" >Please select...</option>
      <%
      
      'loop through recordset to populate the drop down
      Do While Not rsCourses.EOF
            if rsCourses("CourseID") = strURLID then
                        
                        Response.Write ("<option VALUE='" & rsCourses("CourseID") & "' selected>" & rsCourses("CourseTitle") &" - " & rsCourses("CourseDate") & " " & rsCourses("CourseTime") & "</option>")
                        
            else
                        Response.Write ("<option VALUE='" & rsCourses("CourseID") & "'>" & rsCourses("CourseTitle") &" - " & rsCourses("CourseDate") & " " & rsCourses("CourseTime") & "</option>")
            end if

            
      
            rsCourses.MoveNext
      Loop
      rsCourses.close
      set rsCourses = nothing
      %></select>
      </nobr></p>
      <p><font size="2"><b>Course prerequisites:</b> <% if (strURLID <> 0) then response.write rsPreReq("CourseRequirements") %></font></p>

----rest of form here----

<td width="50%"><input type="button" value="Submit" name="B1" onClick="checkWholeForm(this)"></td>
            </tr>
      </table>
</form>

ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial