Domanda

From the following you will see that I have retrieved data from the table school. Next I am trying to derive data from the class table where the class relates to that school. To try and achieve this, I set a variable on a school being selected called Scn.

Alas, for whatever reason I can not get it to read the variable. The variable is confirmed as being set through an alert statement and I can select a class where the school equals (say) 1, but the for the love of all, I can't get the select statement to show data where the class equals the variable.

<SELECT NAME="ListBox1" SIZE=1 onchange="scname(this)">
<% Set conn = Server.CreateObject("ADODB.Connection") %>
<%conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source = \\mockcourt.mdb"
Conn.Open%>



<% Set rs = conn.Execute("SELECT * FROM school") %>
<% Do While Not rs.EOF  ' define the ListBox OPTIONs %>
<OPTION VALUE="<%= rs("schoolKey") %>"> <%= rs("school") %>
<% rs.MoveNext %>
<% Loop %>
<% rs.Close %>
<% conn.Close %>
</SELECT>       

<script type="text/javascript">
function scname(target){
var Scn = target.value
alert("You changed to "+ Scn)
}
</script>

<SELECT NAME="ListBox2" SIZE=1 onChange="clname(this)">
<% Set conn = Server.CreateObject("ADODB.Connection") %>
<%conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source = \\ mockcourt.mdb"
Conn.Open%>
<% Set rs = conn.Execute("SELECT * FROM class WHERE school = '"+Scn+"'") %>
<% Do While Not rs.EOF  ' define the ListBox OPTIONs %>
<% tba=Scn %>
<OPTION VALUE="<%= rs("school") %>,<%= rs("classname") %>"> <%= rs("classname") %>
<% rs.MoveNext %>
<% Loop %>
<% rs.Close %>
<% conn.Close %>
</select>
È stato utile?

Soluzione

Assuming that all your dropdwons is in the form named "myform" and form action either empty or defined as your page, try this:

  <% Set conn = Server.CreateObject("ADODB.Connection") 
 conn.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data source = \\ mockcourt.mdb"
Conn.Open
set rs=Server.CreateObject("ADODB.recordset") 
dim Scn
Scn=request("ListBox1") & ""  ' to make sure that Scn is not null in any case

%>
<select  NAME="ListBox1" onChange="clname(this)">
<% Set rs = conn.Execute("SELECT * FROM school") 
  Do While Not rs.EOF  ' define the ListBox OPTIONs 
 if Scn=rs("schoolKey") then%>
     <OPTION VALUE="<%= rs("schoolKey") %>" selected> <%= rs("school") %></option>
 <% else %>
     <OPTION VALUE="<%= rs("schoolKey") %>"> <%= rs("school") %></option>
<%
  end if
  rs.MoveNext 
  Loop %>

  </SELECT>       
  <% set rs=nothing  %>
<script type="text/javascript">
function scname(target){
var Scn = target.value
alert("You changed to "+ Scn)
document.forms["myform"].submit();
}
</script>

<SELECT NAME="ListBox2" SIZE=1>    
<%
 set rs=Server.CreateObject("ADODB.recordset") 
 Set rs = conn.Execute("SELECT * FROM class WHERE school = '"+Scn+"'")  
  Do While Not rs.EOF  ' define the ListBox OPTIONs 
  tba=Scn %>
<OPTION VALUE="<%= rs("school") %>,<%= rs("classname") %>"> <%= rs("classname") %></option>
<% rs.MoveNext  
  Loop %>

</select>
<% set rs=nothing

  conn.Close %>

it is considered a good practice to have HTML tags, inputs closed even images. And if you planning to ever work within companies where they have to obey certain standard make it your habit.

Altri suggerimenti

Scn is declared within your scname() function. That means its scope is within that function and it cannot be used outside of it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top