学佛网登录:一个有关ASP的问题

来源:百度文库 编辑:高校问答 时间:2024/05/05 23:53:35
本人在制作一个学生成绩录入系统。在存入数据库这一步操作上,本来挺简单的。设计一个表单文件,一个存数据库文件,用action="save.asp"一连接就没事了。但我有个想法能不能将提交表单和存数据库这2个过程合成为1个文件 ,全部在表单文件里实现。就省去了save.asp。也就是说表单文件form.asp里有提交表单还有连接数据库和存数据库相关的语句。谢谢!!
表单文件:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成绩录入系统</title>
<style type="text/css">
<!--
.STYLE1 {
color: #996600;
font-weight: bold;
}
.STYLE2 {font-size: x-large}
-->
</style>
</head>

<body>
<div align="center" class="STYLE1">
<div align="center" class="STYLE2">成绩录入系统</div>
</div>
<form name="form1" method="post">
<label></label>
<p align="center">学号:
<label>
<input name="studynumber" type="text" size="15">
</label>
</p>
<p align="center">姓名:
<label>
<input name="studyname" type="text" size="15">
</label>
</p>
<p align="center">成绩1:
<label>
<input name="result1" type="text" size="15">
</label>
</p>
<p align="center">成绩2:
<label>
<input name="result2" type="text" size="15">
</label>
</p>
<p align="center">成绩3:
<label>
<input name="result3" type="text" size="15">
</label>
</p>
<p align="center">成绩4:
<label>
<input name="result4" type="text" size="15">
</label>
</p>
<p align="center">成绩5:
<label>
<input name="result5" type="text" size="15">
</label>
</p>
<p align="center">
<label>
<input type="submit" name="Submit" value="保存"onclick="save()">
</label>
<label></label>
<input type="reset" name="Submit2" value="清空"><label>
<input type="button" name="Submit3" value="查询"onclick="view()">
</label>
<input type="button" name="Submit4" value="删除" onclick="del()">
</p>
</form>
<script>
function save(){
document.form1.action="save.asp";
document.form1.submit();}
function del(){
document.form1.action="delete.asp";
document.form1.submit();}
function view(){
document.form1.action="view.asp";
document.form1.submit();}
</script>

</body>
</html>

能的,你本来的
<form action=save.asp>
现在
<form action=?action=save>

也就是提交给自己,但是加个变量。

然后,你加个选择结构
if request.querystring("action")="save" then
'下面就是你save.asp的程序
else
'你的表单,显示出来
end if

很好理解吧 ;)

表单中按钮按下时向本身(form.asp)传送参数,在form.asp中对参数进行识别,根据参数再分别进行不同的操作。

<%@ LANGUAGE=VBScript CodePage=936%>
<%Response.Buffer=True%>
<!--#include file="conn_06.asp"-->
<!--#include file="check.asp"-->

<% dim sql
dim rs
action=request("action")
error=request("error")
%>

<%
'删除-------------------------------------------------------------
if action="del" then
tmp=split(request("c1"),", ")
for n = 0 to Ubound(tmp)
sql = "DELETE FROM sort WHERE sort_id = "+tmp(n)+""
conn.execute sql
next
response.redirect "pro_code.asp"
response.end
end if
%>

<%
'添加存储--------------------------------------------
if action="save" then
set rs=server.createobject("adodb.recordset")
sql="select * from sort"
rs.open sql,conn,1,3
rs.addnew
rs("sort_name")=request("topic")
rs("editor")=Request.Cookies("gly_name")
rs("array")=request("array")
rs("date")=Now()
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
response.redirect "pro_code.asp"
response.end
end if
%>

<%
'修改存储--------------------------------------------
if action="editsave" then
set rs=server.createobject("adodb.recordset")
sql="select * from sort where sort_id="&request("sort_id")
rs.open sql,conn,1,3
rs("sort_name")=request("topic")
rs("editor")=Request.Cookies("gly_name")
rs("array")=request("array")
rs("date")=Now()
rs.update
rs.close
set rs=nothing
sql="update produce set sort_name='"&request("topic")&"' where sort_id="&request("sort_id")
conn.execute sql
conn.close
set conn=nothing
response.redirect "pro_code.asp"
response.end
end if
%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<LINK href="inc/style.css" type=text/css rel=stylesheet>
<title>New Page 1</title>
</head>

<SCRIPT LANGUAGE="JavaScript">
<!--//
function check_page()
{
if (isNaN(go2to.page.value))
alert("请正确填写转到页数!");
else if (go2to.page.value=="")
{
alert("请输入转到页数!");
}
else
go2to.submit();
}
//-->
</SCRIPT>

<script language="javascript">
<!--//
function checkall2(){
for(var i=0;i<document.myform.elements.length;i++){
var e=document.myform.elements[i];
if(e.name!='quanxuan2')
e.checked=document.myform.quanxuan2.checked;}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function check()
{
if ( confirm("你确定要删除所选内容吗?")) {document.myform.submit()}
}

//-->
</script>

<body topmargin="10" background="img/mail_di.gif" bgproperties="fixed">
<table border="0" width="780">
<tr>
<td width="100%"><img border="0" src="img/dot.jpg" width="11" height="10">
当前位置:<font color="#FF9900"><font color="#FF9900">产品中心 >> 产品分类<%if action="add" then%> >> 添加分类<%end if%><%if action="edit" then%> >> 修改分类<%end if%></font>     
<%if action="" then%><a href="pro_code.asp?action=add">添加分类</a><%else%><a href="javascript:history.go(-1)">返回</a><%end if%></font>      <a href="produce.asp">产品列表</a></td>
</tr>
<tr>
<td width="100%" background="img/dot_01.jpg" height="1"></td>
</tr>
</table>
<table border="0" width="780">
<tr>
<td width="100%"></td>
</tr>
</table>

<!--添加开始-->
<%if action="add" then
%>
<%if error="code" then%>
<table border="0" width="100%">
<tr>
<td width="780"><font color="#FF0000">请先添加产品分类!</font></td>
</tr>
</table>
<%end if%>
<table border="0" width="780" height="22">
<tr>
<td width="100%" align="center" height="16">
<form method="POST" action="pro_code.asp?action=save" name="add">
<SCRIPT LANGUAGE="JavaScript">
<!--//
function check_add()
{
if (add.topic.value=="") {
alert("请输入分类名称!");
add.topic.focus();
return (false);
}
if (isNaN(add.array.value))
alert("输入正确排列号!");
else if (add.array.value=="")
{
alert("请输入正确排列号!");
}
else
document.add.submit();
}
//-->
</SCRIPT>
<table border="0" width="100%">
<tr>
<td width="12%" align="right">分类名称:</td>
<td width="88%" height="25"><input type="text" name="topic" size="53">
</td>
</tr>
<tr>
<td width="12%" align="right">
排列号:</td>
<td width="88%">
<input type="text" name="array" size="10"> 必须是数字,如1、2、3...</td>
</tr>
<tr>
<td width="12%"></td>
<td width="88%"><input type="button" value=" 确 定 " name="B1" onClick="check_add()" style="height=28"> 
<input type="reset" value=" 重 写 " name="B2" style="height=28"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<%end if%>

<!--添加结束-->

<!--修改开始-->

<%if action="edit" then
set rs=server.createobject("adodb.recordset")
sql="select * from sort where sort_id="&request("sort_id")
rs.open sql,conn,1,1
%>
<table border="0" width="780" height="22">
<tr>
<td width="100%" align="center" height="16">
<form method="POST" action="pro_code.asp?action=editsave&sort_id=<%=request("sort_id")%>" name="edit">
<SCRIPT LANGUAGE="JavaScript">
<!--//
function check_edit()
{
if (edit.topic.value=="") {
alert("请输入分类名称!");
edit.topic.focus();
return (false);
}
if (isNaN(edit.array.value))
alert("输入正确排列号!");
else if (edit.array.value=="")
{
alert("请输入正确排列号!");
}
else
document.edit.submit();
}
//-->
</SCRIPT>
<table border="0" width="100%">
<tr>
<td width="12%" align="right">分类名称:</td>
<td width="88%" height="25"><input type="text" name="topic" size="53" value="<%=rs("sort_name")%>">
</td>
</tr>
<tr>
<td width="12%" align="right">
排列号:</td>
<td width="88%">
<input type="text" name="array" size="10" value="<%=rs("array")%>"> 必须是数字,如1、2、3...</td>
</tr>
<tr>
<td width="12%"></td>
<td width="88%"><input type="button" value=" 确 定 " name="B1" onClick="check_edit()" style="height=28"> 
<input type="reset" value=" 重 写 " name="B2" style="height=28"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<%
rs.close
set rs=nothing
conn.close
set conn=nothing
end if%>

<!--修改结束-->

<!--列表开始-->
<%
if action="" then
set rs=server.createobject("adodb.recordset")
if not isempty(request("page")) then
pagecount=cint(request("page"))
else
pagecount=1
end if
sql="select * from sort order by array desc"
rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write"<p align='center'><font color='#ff0000'>抱歉,没有找到任何记录!</font></p>"
response.end
end if
rs.pagesize=15
if pagecount>rs.pagecount or pagecount<=0 then
pagecount=1
end if
rs.AbsolutePage=pagecount %>

<table border="0" width="780" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="30"><input type="button" value="删除" name="B1" onClick="check()" style="CURSOR: hand"> 
<% if rs.pagecount=1 then %>
共有[<font color="#ff0000"><%=rs.recordcount%></font>]条记录,以下是[<font color="red">1~<%=rs.recordcount%></font>]条<%else%><% page_start=(pagecount-1)*rs.pagesize
if pagecount=1 then page_start=1
page_end=rs.pagesize*pagecount
if pagecount*rs.pagesize=>rs.recordcount then page_end=rs.recordcount end if%>共有[<font color="#ff0000"><%=rs.recordcount%></font>]条记录,以下是[<font color="red"><%=page_start%>~<%=page_end%></font>]条<%end if%></td>
</tr>
<tr>
<td width="100%">
<form method="POST" name="myform" action="pro_code.asp?action=del">
<table border="1" width="100%" bordercolorlight="#CCCCCC" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF">
<tr>
<td width="4%" align="center" height="25" bgcolor="#EBEBEB"><INPUT name=quanxuan2 onclick=checkall2() type=checkbox style="CURSOR: hand"></td>
<td width="44%" align="center" height="25" bgcolor="#EBEBEB">分类名称</td>
<td width="14%" align="center" height="25" bgcolor="#EBEBEB">排列号</td>
<td width="17%" align="center" bgcolor="#EBEBEB">编辑时间</td>
<td width="11%" align="center" bgcolor="#EBEBEB">修改</td>
</tr>
<% do while not rs.eof %>
<tr onmouseout="this.bgColor='#FFFFFF'" onmouseover="this.bgColor='#F0F8FF'">
<td width="4%" height="25" align="center"><input type="checkbox" name="C1" value="<%=rs("sort_id")%>"></td>
<td width="44%" height="25"> <a title="浏览该分类所有产品" href="produce.asp?list=open&sort=<%=rs("sort_id")%>&yy_id=<%=rs("yy_id")%>"><%=rs("sort_name")%></a></td>
<td width="14%" height="25" align="center"><%=rs("array")%></td>
<td width="17%" align="center"><%=rs("date")%></td>
<td width="11%" align="center"><a href="pro_code.asp?action=edit&sort_id=<%=rs("sort_id")%>">修改</a></td>
</tr>
<% i=i+1
rs.movenext
if i>=rs.PageSize then exit do
loop %>
</table>
</form>
</td>
</tr>
<tr>
<td width="100%" align="right">
<% response.write"<form name=go2to form method=Post action='pro_code.asp?action="+action+"'>"
if pagecount=1 then
response.write ""
else
response.write "<a href=pro_code.asp?page=1&action="+action+">首页</a>   "
response.write "<a href=pro_code.asp?page="+cstr(pagecount-1)+"&action="+action+">上一页</a>   "
end if
if rs.PageCount-pagecount<1 then
response.write ""
else
response.write "<a href=pro_code.asp?page="+cstr(pagecount+1)+"&action="+action+">下一页</a>   "
response.write "<a href=pro_code.asp?page="+cstr(rs.PageCount)+"&action="+action+">末页</a>"
end if
response.write "   页次:<font color=blue>"&pagecount&"</font>/"&rs.pagecount&"页"
response.write " 转到第<input type='text' name='page' size=2 maxLength=3 value="&PageCount&">页"
response.write "<input class=button type='button' value='GO!' onclick=check_page()>"
rs.close
set rs=nothing
conn.close
set conn=nothing %>
 </td></form>
</tr>
</table>
<%end if%>
<!--列表结束-->

</body>
</html>

加一条IF 语句判断

上面都不对