在不刷新页面的情况下调用远程asp脚本
程序员文章站
2023-11-04 21:04:40
1.请在sql server 中建一数据库 name:house 在house中运行下列脚本 if exists ...
1.请在sql server 中建一数据库 name:house 在house中运行下列脚本
if exists (select * from sysobjects where id = object_id(n´[dbo].[city]´) and objectproperty(id, n´isusertable´) = 1)
drop table [dbo].[city]
go
if exists (select * from sysobjects where id = object_id(n´[dbo].[area]´) and objectproperty(id, n´isusertable´) = 1)
drop table [dbo].[area]
go
create table [dbo].[city] (
[id] [int] identity (1, 1) not null ,
[name] [nvarchar] (10) null
) on [primary]
go
create table [dbo].[area] (
[id] [int] identity (1, 1) not null ,
[cityid] [int] null ,
[name] [nvarchar] (20) null
) on [primary]
go
insert into city (id,name) values(1,"广州")
insert into city (id,name) values(2,"上海")
insert into city (id,name) values(3,"北京")
insert into area (id,cityid,name) values(1,1,"天河")
insert into area (id,cityid,name) values(2,1,"白云")
insert into area (id,cityid,name) values(3,2,"虹口")
insert into area (id,cityid,name) values(4,2,"宝山")
insert into area (id,cityid,name) values(5,2,"静安")
insert into area (id,cityid,name) values(6,3,"海淀")
insert into area (id,cityid,name) values(7,3,"朝阳")
2.建立文件
建立文件citydata.asp
<%@ language=vbscript %>
<% rsdispatch %>
<script runat=server language=javascript>
<!--#include virtual="/_scriptlibrary/rs.asp"-->
<!--#include virtual="/jsconn.asp"-->
function description()
{
this.getareaasarray = dogetdata;
}
public_description = new description();
function dogetdata(cityid)
{
var sql,rst,strtext,conn,i,j
conn = new activexobject("adodb.connection");
conn.connectionstring = connstr;
conn.connectiontimeout = 30;
conn.open();
sql = "select name,id from area where cityid=´" + cityid + "´";
rst = new activexobject("adodb.recordset");
rst.cursorlocation = 3;
rst.open(sql,conn);
i = 0;
j = 0;
strtext = new array();
if (rst.recordcount >= 1)
{
for(var i=0;i<rst.recordcount;++i)
{
strtext[j++] = rst.fields("name").value;
strtext[j++] = rst.fields("id").value;
rst.movenext();
}
}
else
{
strtext[j++] = "没有";
strtext[j++] = 1;
}
rst.close();
conn.close();
return strtext;
}
</script>
建立文件jsconn.asp
<script runat=server language=javascript>
var connstr = "driver={sql server}; server=202.0.0.108;uid=sa;pwd=;database=house";
</script>
建立文件pub.asp
<%
function openorget_database
dim sessionname, conn
const connstr = "driver={sql server}; server=202.0.0.108;uid=sa;pwd=;database=house"
sessionname = "house"
if not isobject(session(sessionname)) then
set conn = server.createobject("adodb.connection")
conn.open connstr
set session(sessionname) = conn
end if
set openorget_database = session(sessionname)
end function
%>
建立文件sql_pub.asp
<%
´取出城市资料
function selectcity()
dim conn, sql, rs, arrcity, tmparr(1,0)
set conn = openorget_database
set rs = server.createobject("adodb.recordset")
sql = "select distinct name,id from city"
rs.open sql,conn,3
if rs.eof then
tmparr(0,0) = "城市"
tmparr(1,0) = 0
rs.close
selectcity = tmparr
else
arrcity = rs.getrows()
rs.close
selectcity = arrcity
end if
end function
´根据传来的cityid取出相应的地区资料
function selectarea(cityid)
dim conn, sql, rs, arrarea, tmparr(1,0)
set conn = openorget_database
set rs = server.createobject("adodb.recordset")
sql =" select name,id from area where cityid=" & cityid & " order by id "
rs.open sql,conn,3
if rs.eof then
tmparr(0,0) = "地区"
tmparr(1,0) = 0
rs.close
selectarea = tmparr
else
arrarea = rs.getrows()
rs.close
selectarea = arrarea
end if
end function
%>
建立文件test.asp
<%@ language="vbscript" %>
<%option explicit%>
<!--#include virtual="pub.asp"-->
<!--#include virtual="sql_pub.asp"-->
<%
dim arrcity, arrarea, strpathinfo, strservername, strserverport, i, tmpstr
strpathinfo = request.servervariables("path_info")
strservername = request.servervariables("server_name")
strserverport = request.servervariables("server_port")
if len(strserverport) = 0 then
strserverport = ""
else
strserverport = ":" + strserverport
end if
strpathinfo = "http://" & strservername & strserverport & ""
arrcity = selectcity()
arrarea = selectarea(arrcity(1,0))
%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="目录类型" content="文本/html; 字符集=gb2312">
</head>
<script language="javascript" src="<%=strpathinfo%>/_scriptlibrary/rs.htm"></script>
<script language="javascript">
rsenableremotescripting("<%=strpathinfo%>/_scriptlibrary");
</script>
<body bgcolor="#ffffff">
<form method="post" name="select">
<font color="#222200">
<select name="city" size="1" onchange="javascript: choosesection(this.form.area,this.form.city)">
<option value=0 selected>--选择城市--</option>
<%for i = lbound(arrcity,2) to ubound(arrcity,2)
tmpstr = "<option value=" & arrcity(1,i) & ""
tmpstr = tmpstr & ">" & arrcity(0,i) & "</option>"
response.write(tmpstr)
next
%>
</select>
</font><font color="#222200">
<select name="area" size=1>
<option value=0>--选择镇区--</option>
<%for i = lbound(arrarea,2) to ubound(arrarea,2)
if arrarea(1,i)<>0 then
%>
<option value=<%=arrarea(1,i)%>><%=arrarea(0,i)%></option>
<% end if
next%>
</select>
</font>
</form>
<script language="javascript">
var serverurl = "<%=strpathinfo%>";
var pageurl = "/citydata.asp";
function choosesection(oarea,ocity)
{
for(var i=oarea.options.length-1;i>0;--i)
{oarea.options.remove(i) }
var cityid = ocity.options(ocity.selectedindex).value;
var objresult = rsexecute(serverurl+pageurl, "getareaasarray", cityid);
var j=0
if (objresult.return_value.length>0)
{
for (var i=0; i<objresult.return_value.length/2;++i)
{
sname = objresult.return_value[j++];
sid = objresult.return_value[j++];
var ooption = document.createelement(´option´);
ooption.text = sname;
ooption.value = sid;
oarea.options.add(ooption);
}
}
}
</script>
</body>
</html>
if exists (select * from sysobjects where id = object_id(n´[dbo].[city]´) and objectproperty(id, n´isusertable´) = 1)
drop table [dbo].[city]
go
if exists (select * from sysobjects where id = object_id(n´[dbo].[area]´) and objectproperty(id, n´isusertable´) = 1)
drop table [dbo].[area]
go
create table [dbo].[city] (
[id] [int] identity (1, 1) not null ,
[name] [nvarchar] (10) null
) on [primary]
go
create table [dbo].[area] (
[id] [int] identity (1, 1) not null ,
[cityid] [int] null ,
[name] [nvarchar] (20) null
) on [primary]
go
insert into city (id,name) values(1,"广州")
insert into city (id,name) values(2,"上海")
insert into city (id,name) values(3,"北京")
insert into area (id,cityid,name) values(1,1,"天河")
insert into area (id,cityid,name) values(2,1,"白云")
insert into area (id,cityid,name) values(3,2,"虹口")
insert into area (id,cityid,name) values(4,2,"宝山")
insert into area (id,cityid,name) values(5,2,"静安")
insert into area (id,cityid,name) values(6,3,"海淀")
insert into area (id,cityid,name) values(7,3,"朝阳")
2.建立文件
建立文件citydata.asp
<%@ language=vbscript %>
<% rsdispatch %>
<script runat=server language=javascript>
<!--#include virtual="/_scriptlibrary/rs.asp"-->
<!--#include virtual="/jsconn.asp"-->
function description()
{
this.getareaasarray = dogetdata;
}
public_description = new description();
function dogetdata(cityid)
{
var sql,rst,strtext,conn,i,j
conn = new activexobject("adodb.connection");
conn.connectionstring = connstr;
conn.connectiontimeout = 30;
conn.open();
sql = "select name,id from area where cityid=´" + cityid + "´";
rst = new activexobject("adodb.recordset");
rst.cursorlocation = 3;
rst.open(sql,conn);
i = 0;
j = 0;
strtext = new array();
if (rst.recordcount >= 1)
{
for(var i=0;i<rst.recordcount;++i)
{
strtext[j++] = rst.fields("name").value;
strtext[j++] = rst.fields("id").value;
rst.movenext();
}
}
else
{
strtext[j++] = "没有";
strtext[j++] = 1;
}
rst.close();
conn.close();
return strtext;
}
</script>
建立文件jsconn.asp
<script runat=server language=javascript>
var connstr = "driver={sql server}; server=202.0.0.108;uid=sa;pwd=;database=house";
</script>
建立文件pub.asp
<%
function openorget_database
dim sessionname, conn
const connstr = "driver={sql server}; server=202.0.0.108;uid=sa;pwd=;database=house"
sessionname = "house"
if not isobject(session(sessionname)) then
set conn = server.createobject("adodb.connection")
conn.open connstr
set session(sessionname) = conn
end if
set openorget_database = session(sessionname)
end function
%>
建立文件sql_pub.asp
<%
´取出城市资料
function selectcity()
dim conn, sql, rs, arrcity, tmparr(1,0)
set conn = openorget_database
set rs = server.createobject("adodb.recordset")
sql = "select distinct name,id from city"
rs.open sql,conn,3
if rs.eof then
tmparr(0,0) = "城市"
tmparr(1,0) = 0
rs.close
selectcity = tmparr
else
arrcity = rs.getrows()
rs.close
selectcity = arrcity
end if
end function
´根据传来的cityid取出相应的地区资料
function selectarea(cityid)
dim conn, sql, rs, arrarea, tmparr(1,0)
set conn = openorget_database
set rs = server.createobject("adodb.recordset")
sql =" select name,id from area where cityid=" & cityid & " order by id "
rs.open sql,conn,3
if rs.eof then
tmparr(0,0) = "地区"
tmparr(1,0) = 0
rs.close
selectarea = tmparr
else
arrarea = rs.getrows()
rs.close
selectarea = arrarea
end if
end function
%>
建立文件test.asp
<%@ language="vbscript" %>
<%option explicit%>
<!--#include virtual="pub.asp"-->
<!--#include virtual="sql_pub.asp"-->
<%
dim arrcity, arrarea, strpathinfo, strservername, strserverport, i, tmpstr
strpathinfo = request.servervariables("path_info")
strservername = request.servervariables("server_name")
strserverport = request.servervariables("server_port")
if len(strserverport) = 0 then
strserverport = ""
else
strserverport = ":" + strserverport
end if
strpathinfo = "http://" & strservername & strserverport & ""
arrcity = selectcity()
arrarea = selectarea(arrcity(1,0))
%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="目录类型" content="文本/html; 字符集=gb2312">
</head>
<script language="javascript" src="<%=strpathinfo%>/_scriptlibrary/rs.htm"></script>
<script language="javascript">
rsenableremotescripting("<%=strpathinfo%>/_scriptlibrary");
</script>
<body bgcolor="#ffffff">
<form method="post" name="select">
<font color="#222200">
<select name="city" size="1" onchange="javascript: choosesection(this.form.area,this.form.city)">
<option value=0 selected>--选择城市--</option>
<%for i = lbound(arrcity,2) to ubound(arrcity,2)
tmpstr = "<option value=" & arrcity(1,i) & ""
tmpstr = tmpstr & ">" & arrcity(0,i) & "</option>"
response.write(tmpstr)
next
%>
</select>
</font><font color="#222200">
<select name="area" size=1>
<option value=0>--选择镇区--</option>
<%for i = lbound(arrarea,2) to ubound(arrarea,2)
if arrarea(1,i)<>0 then
%>
<option value=<%=arrarea(1,i)%>><%=arrarea(0,i)%></option>
<% end if
next%>
</select>
</font>
</form>
<script language="javascript">
var serverurl = "<%=strpathinfo%>";
var pageurl = "/citydata.asp";
function choosesection(oarea,ocity)
{
for(var i=oarea.options.length-1;i>0;--i)
{oarea.options.remove(i) }
var cityid = ocity.options(ocity.selectedindex).value;
var objresult = rsexecute(serverurl+pageurl, "getareaasarray", cityid);
var j=0
if (objresult.return_value.length>0)
{
for (var i=0; i<objresult.return_value.length/2;++i)
{
sname = objresult.return_value[j++];
sid = objresult.return_value[j++];
var ooption = document.createelement(´option´);
ooption.text = sname;
ooption.value = sid;
oarea.options.add(ooption);
}
}
}
</script>
</body>
</html>
上一篇: SQL字符串处理函数大全
推荐阅读