#12002/10/20 21:59:45
一步一步学做校友录之创建班级篇
在这一篇我们将实现创建班级的简单功能,我们需要在class,admin,user表里分别写入创建人提交的数据。
首先你得提供index.asp让人来创建班级。演示地址:http://gu9808.126.com
代码如下:
<!--#include file="conn.asp"-->
<!--打开class表-->
<% sql="select * from class order by date desc"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
if rs.eof or rs.bof then
response.write("没有班级")
end if%>
<html>
<head>
<title>飞灵校友录</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
<script>function votedo(){OpenWindow=window.open('','votedo','toolbar=no,scrollbars=1,menubar=no,resizable=1,width=400,height=200')};function voteview(){OpenWindow=window.open('vote.asp','votedo','toolbar=no,scrollbars=1,resizable=1,menubar=no,width=400,height=207')};</script>
<script>function voteview(){OpenWindow=window.open('','voteview','toolbar=no,scrollbars=1,menubar=no,resizable=1,width=400,height=200')};function voteview(){OpenWindow=window.open('votedetail.asp','votedo','toolbar=no,scrollbars=1,resizable=1,menubar=no,width=400,height=207')};</script>
<script language="javascript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<script language="javascript">
function checkit()
{
var wang1=form1.classname.value;
var wang2=form1.name.value;
var wang3=form1.pass.value;
var wang4=form1.pass1.value;
var wang5=form1.tc.value;
if(wang1==""){alert ("班级名字不能为空");return false;}
if(wang2==""){alert ("用户名不能为空");return false;}
if(wang3==""){alert ("密码不能为空");return false;}
if(wang4==""){alert ("验证密码不能为空");return false;}
if(wang5==""){alert ("班主任老师不能为空");return false;}
return true;
}
</script>
<link rel="stylesheet" href="../css/fe.css" type="text/css"></head>
<body bgcolor="#ececec" background="images/bg11.gif" text="#000000" leftmargin="0" topmargin="0" onLoad="MM_preloadImages('images/1-.gif','images/6-.gif','images/7-.gif','images/3-.gif','images/2-.gif','images/4-.gif','images/5-.gif')">
<!--#include file="top.asp"-->
<table width="750" height="10" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#ececec">
<tr>
<td height="10" style="border-left:#000000 0px solid; width="169" > </td>
<td width="581" height="10"><a href="../index2.asp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image1','','images/1-.gif',1)"><img name="Image1" border="0" src="images/1.gif" width="83" height="10"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image34','','images/3-.gif',1)"><img src="images/3.gif" name="Image34" width="83" height="10" border="0"></a><a href="../qg/qg.htm" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image35','','images/2-.gif',1)"><img src="images/2.gif" name="Image35" width="83" height="10" border="0"></a><a href="../yule/index.asp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image36','','images/4-.gif',1)"><img src="images/4.gif" name="Image36" width="83" height="10" border="0"></a><a href="http://hollyway.com/feeling/bbs" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image37','','images/5-.gif',1)"><img src="images/5.gif" name="Image37" width="83" height="10" border="0"></a><a href="../liuyan/index.asp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','images/6-.gif',1)"><img name="Image6" border="0" src="images/6.gif" width="83" height="10"></a><a href="index.asp" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','images/7-.gif',1)"><img name="Image7" border="0" src="images/7.gif" width="83" height="10"></a></td>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center" height="400">
<tr>
<td width="139" height="400" align="center" valign="top" bgcolor="#ececec" style="border-left:#000000 0px solid;border-right:#000000 0px solid width="139"><table width="105" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center"><img src="images/menu.gif" width="98" height="46"></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu2.gif" width="98" height="20"></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu3.gif" width="98" height="23"></div></td>
</tr>
<tr>
<td><div align="center"><a href="gl.asp"><img src="images/menu4.gif" width="98" height="23" border="0"></a></div></td>
</tr>
<tr>
<td><div align="center"><a href="userlist.asp"><img src="images/menu5.gif" width="98" height="22" border="0"></a></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu6.gif" width="98" height="21"></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu7.gif" width="98" height="21"></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu8.gif" width="98" height="21"></div></td>
</tr>
<tr>
<td><div align="center"><img src="images/menu9.gif" width="98" height="23"></div></td>
</tr>
</table>
<!--显示最新加入的10个班级-->
<table width="135" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="15"><img src="images/folderhot.gif" width="16" height="16">最新加入班级</td>
</tr>
<tr>
<%for i=1 to 10
if rs.eof then exit for end if %>
<td align="center"><a title="<%=rs("classname")%>" href="index1.asp?classid=<%=rs("classid")%>">
<% if len(rs("classname"))>=6 then %>
<%=left(rs("classname"),6) %>...
<%else%>
<%=rs("classname")%>
<%end if%>
</a></td>
</tr>
<%rs.movenext
next%>
</table>
</td>
<td width="1" background="images/beijing.gif" bgcolor="#ececec" > </td>
<td width="610" valign="top">
<table width="610" border="0" cellspacing="0" cellpadding="0" height="20">
<tr>
<td width="249" height="13"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%"><img src="images/dian.gif" width="14" height="12">班级论坛新贴<img src="images/dian.gif" width="14" height="12"></td>
<td width="50%" align="right"><img src="images/dian.gif" width="14" height="12"><a href="/bbs/forum.asp?nForumID=18">进入班级论坛</a><img src="images/dian.gif" width="14" height="12"></td>
</tr>
</table></td>
<td rowspan="2" width="1" background="images/beijing.gif"> </td>
<td width="310"><table width="248" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td width="50"><img src="images/words.gif" width="51" height="13"></td>
<td width="50"><a href="userlist.asp"><img src="images/classmate.gif" width="88" height="13" border="0"></a></td>
<td width="50"><img src="images/aboat.gif" width="49" height="13"></td>
<td width="50"><a title="给斑竹发个EMAIL"href="mailto:gu9808@sina.com"><img src="images/email.gif" width="58" height="13" border="0"></a></td>
</tr>
</table> </td>
</tr>
</table>
<!--提供登陆页面,将在以后提到-->
<table width="610" border="0" cellspacing="0" cellpadding="0" height="1">
<tr>
<td height="1" background="images/bo-bg.gif"></td>
</tr>
</table>
<table width="610" border="0" cellspacing="1" cellpadding="0">
<tr>
<td height="400" valign="top" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="60" height="15" align="center" bgcolor="#ebeaf2"><img src="images/login.gif" width="50" height="12"></td>
<form name="login" method="post" action="login.asp">
<td width="170" align="center" bgcolor="#ebeaf2"> 用户名:
<input name="name" type="text" class=cssEditCtrl size="15">
</td>
<td width="140" bgcolor="#ebeaf2">密码:
<input name="pass" type="password" class=cssEditCtrl size="12">
</td>
<td width="30" bgcolor="#ebeaf2"><input type="submit" name="login" value="确定" class=cssEditCtrl></td>
<td width="210" bgcolor="#ebeaf2"> </td>
</form>
</tr>
<tr>
<td height="1" colspan="5" background="images/bo-bg.gif"></td>
</tr>
</table>
<table width="100%" height="400" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" background="images/bg7.gif" width="440" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/missyou.gif" width="195" height="64"></td>
<td width="60%" valign="baseline">
<form name="form2" method="post" action="serchclass.asp">
<input name="what" type="text" id="what" value="在这里班级名称" size="30" maxlength="20">
<input type="submit" name="Submit5" value="搜索">
</form>
</td>
</tr>
</table>
<!--分页显示班级列表,按时间的倒序-->
<table width="95%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td align="center">以下是班级列表,看看你的班级在里面吗?</td>
</tr>
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">
<%rs.pagesize=8
page=request("page")
if page=""or page<1 then page=1 end if
rs.absolutepage=page
for i=1 to rs.pagesize
if rs.eof then exit for end if%>
<%if bg=""or bg="#ffffff"then
bg="#e6e6e6"
else bg="#ffffff"
end if%>
<tr bgcolor=<%=bg%>>
<td width="50" height="20" >
<div align="center"><%=rs("classid")%></div>
</td>
<td width="100" height="20" >
<div align="center"><a href="index1.asp?classid=<%=rs("classid")%>"><%=rs("classname")%></a></div>
</td>
<td width="100" height="20" >
<div align="center">
<% =rs("date")%>
</div>
</td>
</tr>
<%rs.movenext
next%>
</table>
</td>
</tr>
</table>
<table width="440" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="middle" align="center">
<%if page<>1 then %>
<a href=index.asp.asp?page=1>首页</a>
<%end if%>
</td>
<td valign="middle" align="center">
<%if page>1 then %>
<a href=index.asp.asp?page=<%=page-1%>>上页</a>
<%end if%>
</td>
<td valign="middle" align="center">
<%if page<>rs.pagecount then %>
<a href=index.asp.asp?page=<%=page+1%>>下页</a>
<%end if%>
</td>
<td valign="middle" align="center">
<%if page<>rs.pagecount then %>
<a href=index.asp.asp?page=<%=rs.pagecount%>>末页</a>
<%end if%>
</td>
<td valign="middle" align="center">这是第<%=page%>页</td>
<td valign="middle" align="center">一共有<%=rs.pagecount%>页</td>
</tr>
</table>
<!--调用checkit()在客户端判断数据的完整,然后交给creatclass.asp来处理-->
<table width="80%" border="0" cellspacing="0" cellpadding="0" height="20">
<tr>
<td align="center"><%=session("info")%></td>
</tr>
</table>
</td>
<td valign="top" background="images/bg7.gif">
<form name="form1" method="post" onSubmit="return checkit()" action="creatclass.asp">
<table width="90%" border="0" cellspacing="0" cellpadding="2" align="center">
<tr>
<td align="center">创 建 新 班 级</td>
</tr>
<tr>
<td height="20"><img src="images/page-1-1.gif" width="16" height="13"> 新班级名称:</td>
</tr>
<tr>
<td align="center" height="20">
<input type="text" name="classname" size="18" maxlength="50">
</td>
</tr>
<tr>
<td height="20"><img src="images/page-2.gif" width="17" height="13"> 选择管理员姓名:</td>
</tr>
<tr>
<td height="20" align="center">
<input type="text" name="name" size="15" maxlength="50">
</td>
</tr>
<tr>
<td height="20"><img src="images/three.gif" width="16" height="13"> 管理员密码:</td>
</tr>
<tr>
<td height="20" align="center">
<input type="password" name="pass" size="15" maxlength="50">
</td>
</tr>
<tr>
<td height="20"><img src="images/four.gif" width="17" height="13"> 验证密码:</td>
</tr>
<tr>
<td height="20" align="center">
<input type="password" name="pass1" size="15" maxlength="50">
</td>
</tr>
<tr>
<td height="20"><img src="images/five.gif" width="16" height="13"> 高中班级班主任的姓</td>
</tr>
<tr>
<td height="20" align="center">
<input type="text" name="tc" size="15" maxlength="50">
</td>
</tr>
<tr>
<td height="20" align="center">
<input type="submit" name="Submit" value="确定">
</td>
</tr>
</table>
</form>
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center" height="80">
<tr>
<td background="images/BO-BG.GIF">
<table width="100%" border="0" cellspacing="1" cellpadding="0" height="80">
<tr>
<td bgcolor="#FFFFFF" valign="top">
注意:在新建班级以前请你
先搜索一下班级是否存在,
以免造成系统资源的浪费!</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table></td>
</tr>
</table>
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
</table><!--#include file="bottom.asp"-->
</body>
</html>
以下是creatclass.asp的代码:
<!--连接数据库"-->
<!--#include file="conn.asp"-->
<% classname=request("classname")
name=request("name")
pass=request("pass")
pass1=request("pass1")
tc=request("tc")
'检查密码
if pass1<>pass then
response.write("前后密码不一样!!")
response.end
end if
'判断班级是否已经存在
sql="select * from class where classname='"&classname&"'"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
if not rs.eof or not rs.bof then
response.write("此班级已经存在!")
response.end
end if
'判断管理员的名称是否已经存在
sql1="select * from admin user where name='"&name&"'"
set rs1=server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,3
if not rs1.eof then
response.write("此用户已经存在!")
response.end
end if
'判断用户名是否已经存在
sql2="select * from user where name='"&name&"'"
set rs2=server.CreateObject("adodb.recordset")
rs2.open sql2,conn,1,3
if not rs2.eof then
response.write("此用户已经存在!")
response.end
end if
'注册班级
rs.addnew
rs("classname")=classname
rs.update
classid=rs("classid")
rs1.addnew
rs1("name")=name
rs1("pass")=pass
rs1("classid")=classid
rs1.update
rs1.close
rs2.addnew
rs2("name")=name
rs2("pass")=pass
rs2("tc")=tc
rs2("classid")=classid
rs2.update
rs2.close
rs.close
'session("info")将在首页出现,提示用户注 册 班 级 成 功,并提供登陆连接
session("info")="注 册 班 级 成 功! <a href='index1.asp?classid="&classid&"'>进 入 新 建 的 班 级</a>"
response.redirect("index.asp")
conn.close
set conn=nothing
%>
家伟觉得creatclass.asp的代码可以更好一点,希望大家提供更优化的代码
编辑历史:[此帖最近一次被 allinhands 编辑过(编辑时间:2004-12-01 15:16:05)]