Print Page | Close Window

Dynamic Dropdown issue

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=4302
Printed Date: 30 March 2026 at 10:07am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Dynamic Dropdown issue
Posted By: simflex
Subject: Dynamic Dropdown issue
Date Posted: 17 July 2003 at 9:47am

<html>
<head>
<language=javascript>
function setOptions(optSelect) {
var selbox = document.frmNew.Section;

selbox.options.length = 0;
if (optSelect == " ") {
  optSelect.options[optSelect.options.length] = new Option('Please select one of the options above first',' ');
    }


if (optSelect == "ORG") {
   <%
   'lets queueu up the recordset, check if it has records, then output some options
   If NOT staffset.EOF Then staffset.MoveFirst
   Do Until staffset.EOF
      'lets dynamically add to the javascript
      Response.Write "selbox.options[selbox.options.length] = new Option('" & staffset("empid") & "','" & staffset("lname") & "');" & vbCrLf
      staffset.MoveNext
   Loop
   %>
   }
  
 }
</script>

</head>
<body>
Set emailDB = Server.CreateObject("ADODB.Connection")
emailDB.Open "dsn=to_odbc"
<form name=frmNew action=insertRec method=post>
  <table>
    <tr>
      <td>Name of Affected Deputy Director?</td>
      <td> <select name="staff" onchange="setOptions(document.frmNew.staff.options[document.frmNew.staff.selectedIndex].value);">
        <OPTION value="invalid choice" SELECTED>&lt;Choose One&gt;</OPTION>
          <%
sql = "SELECT theEmp.empID, theEmp.LName+', '+theEmp.FName as fullname FROM theEmp WHERE theEmp.TypeID = 2"
set staffset = emailDB.execute(sql)
while not staffset.eof
%>
   <option value="<%=staffset(0)%>"><%=staffset(1)%></option>
  <%
 staffset.Movenext
 wend
 staffset.close
 set staffset = nothing

    %>
  </select></td>
</tr>
</table>
</form>
</body>
</html>




Replies:
Posted By: michael
Date Posted: 17 July 2003 at 10:24am
Am I missing something or...? Where are you putting your records into an array? You should use staffset("empID") and staffset("Fullname") to get the values.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: simflex
Date Posted: 17 July 2003 at 11:50am

hi Michael,

when I put the javascript code between the head  <head></head>of the html code, I get "object required" script error.

If I put it just below this line:

Set emailDB = Server.CreateObject("ADODB.Connection")
emailDB.Open "dsn=to_odbc"

I get:

"expected statement 

<language="javascript">" error even after this:

      Response.Write "selbox.options[selbox.options.length] = new Option('" & staffset("empid") & "','" & staffset("fullname") & "');" & vbCrLf

I am not sure where these pieces of code should be placed.

 



Posted By: MorningZ
Date Posted: 17 July 2003 at 12:14pm

i think (and this is by looking at the above code) that you don't understand that the ASP's VBscript runs on the server before the html gets put to the client....

so right off the bat, you need to take the ADODB.Connection and the call to fill the recordset to the very top of the page so that is availabel when it tries to Response.Write out the javascript that will then be run on the client.......

 



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: KCWebMonkey
Date Posted: 17 July 2003 at 6:32pm
Also, make sure that your ASP code is between <% and %> or it will not be viewed as server side script.


Posted By: simflex
Date Posted: 18 July 2003 at 7:08am

*************************************************

i think (and this is by looking at the above code) that you don't understand that the ASP's VBscript runs on the server before the html gets put to the client....

*****************************************

I do and I had the adodb and the call at the top in my original code.

Sometimes when you try what you know to solve a problem and is not working, then you begin to tinker with the code to see if you can get lucky and resolve the problem.

Such was the case with me with this code.

Well, I have put it back at the top and I am still getting this error"

"object required" around this code:

If NOT staffset.EOF Then staffset.MoveFirst
   Do Until staffset.EOF
      'lets dynamically add to the javascript
      Response.Write "selbox.options[selbox.options.length] = new Option('" & staffset("empid") & "','" & staffset("lname") & "');" & vbCrLf
      staffset.MoveNext
   Loop
This is frustrating.

If I do a static dropdown where the values are hardcoded, it works but I have to do it this way because there is an email program attached to my code which sends email based on someone's empid.

If I do it static, the empid will have to be removed.



Posted By: MorningZ
Date Posted: 18 July 2003 at 8:12am

again.. you are declaring/filling the recordset "staffset" at the bottom of the page

but you are trying to use it at the very top of the page... the ASP engine has absolutely no idea what "staffset" is at that point.. you must declare the connection, open the connection, delcare the recordset, open the recordset, THEN you can use it....

your code is totally out of order



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: MorningZ
Date Posted: 18 July 2003 at 8:21am

heres the code redone

http://www.morningz.com/sections/playground/files/code/proper_flow.zip - http://www.morningz.com/sections/playground/files/code/proper_flow.zip

no idea if it works since i dont have your database... also fixed the script block for javascript that you had tagged wrong



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: simflex
Date Posted: 18 July 2003 at 12:09pm

thank you very much for the help and your time.

The error is gone but I am not getting any values.

Forgive for Just a little bit more info.

There is a table called empType.

This table has 2 fields: typeID and type.

TypeID is either 1, 2, or 3.

Then the Type field comprises Three levels of employees:

The deputy for resolving departmental issues; the associated deputy.

This deputy resolves issues relating to his/her section.

This deputy has a typeID of 2.

So when a name is selected from the emp table (another table), any names under staffset of dropdown menu that matches typeID 2 is populated into this dropdown.

Also each staffset is associated with a particular org code (this is also in the emp table).

Org codes range from 5401 to 5414.

If for instance, I select org code 5401 from the org dropdown menu, I expect to see associated deputy called John Doe to be automatically be populated into the staffset dropdown menu.

Please don't be upset if I paste the code.

The intent is to show the logical flow of things.

This incorporates the changes you made to the code.

My hope is that you can show why staffset is not getting populated with values from org.

 

<%@ Language="VBScript"%>
<%
Set emailDB = Server.CreateObject("ADODB.Connection")
emailDB.Open "dsn=to_odbc"

sql = "SELECT theEmp.empID, theEmp.LName+', '+theEmp.FName as fullname FROM theEmp WHERE theEmp.TypeID = 3"
set Supervisorset = emailDB.execute(sql)

'Definte and open the recordset
sql = "SELECT theEmp.empID, theEmp.LName+', '+theEmp.FName as fullname FROM theEmp WHERE theEmp.TypeID = 2"
set staffset = emailDB.execute(sql)

sql = "SELECT theEmp.empID, theEmp.lname+','+theEmp.fname as fullname FROM theEmp WHERE theEmp.TypeID = 1"
set Deputyset = emailDB.execute(sql)

%>
<head>
<title>Untitled</title>
<script language="javascript">
function setOptions(optSelect) {
 var selbox = document.frmNew.staff;
 selbox.options.length = 0;
 if (optSelect == " ") {
  optSelect.options[optSelect.options.length] = new Option('Please select one of the options above first',' ');
 }
 if (optSelect == "ORG") {
 <%
 'lets queueu up the recordset, check if it has records, then output some options
 Do Until staffset.EOF
  'lets dynamically add to the javascript
  Response.Write "selbox.options[selbox.options.length] = new Option('" & staffset("empID") & "','" & staffset("fullName") & "');" & vbCrLf
  staffset.MoveNext
 Loop

 'Now reset the recordset so we can use further down
 staffset.MoveFirst
 %>
 }
}
</script>
</head>

<body>

    <tr>
      <td>Select Organization Code:</td>
      <td width="41%">
       <select name="ORG"  size="1" onchange="setOptions(document.frmNew.ORG.options[document.frmNew.ORG.selectedIndex].value);">
        <option value="5401">5401</option>
        <option value="5402">5402</option>
        <option value="5403">5403</option>
        <option value="5404">5404</option>
        <option value="5406">5406</option>
        <option value="5408">5408</option>
        <option value="5414">5414</option>
      </select>
    <%if errorcode3 = "yes" then%> <font color="#FF0000"><strong>*please insert the Organization Code</strong> </font> <%end if%> </td>
    </tr>
    <tr>
      <td>Name of the immediate supervisor?</td>
      <td> <select name="Supervisor">
      <OPTION value="-1" SELECTED>&lt;Choose One&gt;</OPTION>
          <%
while not Supervisorset.eof
%>
          <option value="<%=Supervisorset(0)%>"><%=Supervisorset(1)%></option>
          <%
 Supervisorset.Movenext
 wend
 Supervisorset.close
 set Supervisorset = nothing

%>
        </select></td>
    </tr>
    <tr>
 <td>Name of Affected Deputy Director?</td>
 <td>
  <select name="staff" >
  <%
  Do while not staffset.eof
  %>
  <option value="<%=staffset(0)%>"><%=staffset(1)%></option>
  <%
   staffset.MoveNext
  Loop
  'Now we're done, let's release all the objects
  staffset.close
  set staffset = nothing
  %>
  </select>
 </td>
  </tr>
    <tr>
      <td>Name of the Deputy Director? </td>
      <td> <select name="Deputy">
          <%
    while not Deputyset.eof
         %>
          <option value="<%=Deputyset(0)%>"><%=Deputyset(1)%></option>
          <%
 Deputyset.Movenext
 wend
 Deputyset.close
 set Deputyset = nothing

  %>
</select></td>
    </tr>
  </table>
</form>
</body>
<%

'free up resources.
emaildb.close
set emaildb = nothing
%>




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net