I am writing a program for dynamically linked 2 listboxes with VBScript & MS Access. There are contained 2 asp files, one is called newsSearchForm1, another is newsSearchForm2 and my database is NewsData.mdb.
1st file (NewsSearchForm1.asp):
<script language="JavaScript">
var serverURL = 'newsSearchForm2.asp';
function myCallBack(co) {
if (co.status != -1) {
eval(co.return_value);
}
}
function GetModels() {
RSExecute(serverURL
,'RSGetModel'
,'document.forms[0].QryStr4'
,document.forms[0].QryStr3.options[document.forms[0].QryStr3.selectedIndex].value
,myCallBack(co));
}
</script>
<form>
<table border="0" width="100%">
<tr>
<td>&nbsp;&nbsp;&nbsp;</td>
<td><p align="right">Category Code: </p></td>
<td></td>
<td>
<select name=QryStr3 onChange="GetModels()">
<option value="0">Select Category Code
<%
myconn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("NewsData.mdb" & ";"
myquery="select distinct Category from Category"
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection"
conntemp.open myconn
set rstemp=conntemp.execute(myquery)
do while not rstemp.eof
fieldfirst=RStemp(0)
if isnull(fieldfirst) or fieldfirst="" then
' ignore
else
response.write "<option value='" & fieldfirst
response.write "'>" & fieldfirst & "</option>"
end if
rstemp.movenext
loop
%>
</select>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;</td>
<td><p align="right">MICode: </p></td>
<td></td>
<td>
<select name=QryStr4>
<option value="0">Select MICode
</td>
</tr>
</form>
2nd file (NewsSearchForm2):
<%RSDispatch%>
<SCRIPT RUNAT=SERVER Language=javascript>
function Description()
{
this.RSGetModel = Function("lbname","Qrystr3","return RSGetModels(lbname,QryStr3)"
}
public_description = new Description();
</SCRIPT>
function RSGetModels(lbname,QryStr3)
dim SQL
SQL="select MICode from Category where Category = " & QryStr3
RSGetModels=RemoteScriptListBox(lbname,SQL)
end function
function jq(str)
dim ret
ret=str & ""
ret=replace(ret,"\","\\" 'escape any backslashs
ret=replace(ret,"'","\'" 'escape any single quotes
ret=replace(ret,"""","\""" 'escape any double quotes
jq="'" & ret & "'" 'surround with single quotes
end function
function RemoteScriptListBox(lbname,SQL)
dim comma,x,conn,rx,col1,col2
dim ret
set conn=server.CreateObject("ADODB.Connection"
myconn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("NewsData.mdb" & ";"
conn.open myconn
comma=""
x=0
ret = ret & "var lb=" & lbname & ";"
'--create remote array
ret = ret & "var _o0=new Array("
Set rx = conn.Execute(SQL)
do while not rx.eof
col1=cstr(rx(0) & ""
col2=trim(cstr(rx(1) & "")
ret = ret & comma & jq(col2) & "," & jq(col1)
comma=","
x=x+1
rx.movenext
loop
rx.close
conn.close
ret = ret & ""
ret = ret & "lb.length=0;"
ret = ret & "var x=0;"
ret = ret & "while (x != _o0.length) {"
ret = ret & "lb.options[lb.length] = new Option(_o0[x],_o0[x+1]);"
ret = ret & "x=x+2;}"
RemoteScriptListBox = ret
End function
%>
A problem is when I chose the first listbox, there is no response for the second listbox. Can someone tell me what's wrong with my code?
1st file (NewsSearchForm1.asp):
<script language="JavaScript">
var serverURL = 'newsSearchForm2.asp';
function myCallBack(co) {
if (co.status != -1) {
eval(co.return_value);
}
}
function GetModels() {
RSExecute(serverURL
,'RSGetModel'
,'document.forms[0].QryStr4'
,document.forms[0].QryStr3.options[document.forms[0].QryStr3.selectedIndex].value
,myCallBack(co));
}
</script>
<form>
<table border="0" width="100%">
<tr>
<td>&nbsp;&nbsp;&nbsp;</td>
<td><p align="right">Category Code: </p></td>
<td></td>
<td>
<select name=QryStr3 onChange="GetModels()">
<option value="0">Select Category Code
<%
myconn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("NewsData.mdb" & ";"
myquery="select distinct Category from Category"
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection"
conntemp.open myconn
set rstemp=conntemp.execute(myquery)
do while not rstemp.eof
fieldfirst=RStemp(0)
if isnull(fieldfirst) or fieldfirst="" then
' ignore
else
response.write "<option value='" & fieldfirst
response.write "'>" & fieldfirst & "</option>"
end if
rstemp.movenext
loop
%>
</select>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;</td>
<td><p align="right">MICode: </p></td>
<td></td>
<td>
<select name=QryStr4>
<option value="0">Select MICode
</td>
</tr>
</form>
2nd file (NewsSearchForm2):
<%RSDispatch%>
<SCRIPT RUNAT=SERVER Language=javascript>
function Description()
{
this.RSGetModel = Function("lbname","Qrystr3","return RSGetModels(lbname,QryStr3)"
}
public_description = new Description();
</SCRIPT>
function RSGetModels(lbname,QryStr3)
dim SQL
SQL="select MICode from Category where Category = " & QryStr3
RSGetModels=RemoteScriptListBox(lbname,SQL)
end function
function jq(str)
dim ret
ret=str & ""
ret=replace(ret,"\","\\" 'escape any backslashs
ret=replace(ret,"'","\'" 'escape any single quotes
ret=replace(ret,"""","\""" 'escape any double quotes
jq="'" & ret & "'" 'surround with single quotes
end function
function RemoteScriptListBox(lbname,SQL)
dim comma,x,conn,rx,col1,col2
dim ret
set conn=server.CreateObject("ADODB.Connection"
myconn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("NewsData.mdb" & ";"
conn.open myconn
comma=""
x=0
ret = ret & "var lb=" & lbname & ";"
'--create remote array
ret = ret & "var _o0=new Array("
Set rx = conn.Execute(SQL)
do while not rx.eof
col1=cstr(rx(0) & ""
col2=trim(cstr(rx(1) & "")
ret = ret & comma & jq(col2) & "," & jq(col1)
comma=","
x=x+1
rx.movenext
loop
rx.close
conn.close
ret = ret & ""
ret = ret & "lb.length=0;"
ret = ret & "var x=0;"
ret = ret & "while (x != _o0.length) {"
ret = ret & "lb.options[lb.length] = new Option(_o0[x],_o0[x+1]);"
ret = ret & "x=x+2;}"
RemoteScriptListBox = ret
End function
%>
A problem is when I chose the first listbox, there is no response for the second listbox. Can someone tell me what's wrong with my code?