Professional Documents
Culture Documents
Tai Lieu Lap Trinh ASP 02
Tai Lieu Lap Trinh ASP 02
2.1 Registration
Registration l module cho php mt khch vng lai ng k lm thnh vin ca website. Module ny gm mt form ng k thnh vin, 1 file asp x l form ny, insert d liu vo database. database c mt table tblUser cha danh sch cc thnh vin ca website
Hnh 2.1 RegistrationForm.htm: trang ny cha form cho php ngi dng ng k. RegistrationProcess.asp: trang ny x l d liu t form trn, nu hp l th insert d liu vo database Ngoi ra, kt ni vo database chng ta vit 1 file connection.asp cha cc hm open v destroy connection ri include file ny vo cc file c nhu cu truy cp database. Trong Database cha table : tblUser
28
ASP
Trang RegistrationForm.htm <html> <head> <title>Registration</title> </head> <body> <form method="POST" action="RegistrationProcess.asp"> <p> Username: <input type="text" name="username ></p> <p> Password: <input type=password" name="password ></p> <p> Confirm Password: <input type=password" name="ConfirmPassword ></p> <p> Address: <input type="text" name="address"></p> <p><input type="submit" value="Submit" name=submit"></p> </form> </body> </html> Trang Connection.asp <% dim conn Sub openConn() hm m connection ti DB set conn=server.createobject("adodb.connection") connstr="provider=microsoft.jet.oledb.4.0; source="&server.mappath("myDB.mdb")&";" conn.open connstr End Sub Sub destroyConn() conn.close set conn=nothing End Sub %> hm ng v hy connection
data
Trang RegistrationProcess.asp <!--#include file ="Connection.asp"--> <% username=request.form("username") password=request.form("password") confirmPassword=request.form("confirmPassword") address=request.form("address") validate some information retrieved from submitted form openConn sql="insert into tblUser([username],[password],[address]) values('"&username&"','"&password&"','"&address&"')" conn.execute sql
29
Hnh 2.3
LoginForm.htm: Form login LoginProcess.asp: x l form login, nu login thnh cng thi redirect ti trang Index.asp,nu khng th quay li form login. Index.asp: Trang ch ch dnh cho member login bng cch kim tra bin session, nu bin ny rng (cha login) th t chi truy cp v redirect n form login Logout.asp: Trang x l logout bng cch hy session Trang LoginForm.html <html>
30
ASP
<head> <title>Login</title> </head> <body> <form method="POST" action="LoginProcess.asp"> <p> Username: <input type="text" name="username"></p> <p> Password: <input type="password" name="password"></p> <p><input type="submit" value="Submit" name=submit"></p> </form> </body> </html> Trang LoginProcess.asp <!--#include file ="Connection.asp"--> <% username=request.form("username") password=request.form("password") openConn sql="select * from tblUser where username='"&username&"' password='"&password&"'" set rs=server.createobject("adodb.recordset") rs.open sql,conn if not rs.eof then login thnh cng session("username")=rs("username") rs.close destroyConn response.redirect "index.asp" else login tht bi session("username")="" rs.close destroyConn response.redirect "LoginForm.html" end if %>
and
Trang Index.asp <html> <head> <title>Home page for Member only</title> </head> <body> <% if session("username")="" then kim tra ngi dng login cha? response.redirect "LoginForm.html" end if%> Welcome to <%=session("username")%>. This page is for Member only! <a href="Logout.asp"> Logout</a> </body>
31
Hnh 2.4 Cc phn cn li gm cc trang sau: ListMember.asp: Lit k danh sch thnh vin, vi mi thnh vin c cc lin kt cho php sa v xa thnh vin .
32
ASP
EditMemberForm.asp: form sa thnh vin, hin th cc thng tin hin ti ca thnh vin ngi dng c th sa. EditMemberProcess.asp: x l form sa thnh vin, update li thnh vin vo DB DeleteMember.asp: xa thnh vin Trang ListMember.asp <!--#include file ="Connection.asp"--> <%'if session("username")="" then response.redirect "LoginForm.html"%> <% openConn set rs = server.createobject("ADODB.Recordset") rs.open "select * from tblUser", conn%> <table border="1" width="200"> <tr><td>ID</td><td>Username</td><td>Address</td><td>Edit</ td><td>Delete</td></tr> <% do while not rs.EOF link1 = "EditMemberForm.asp?id=" & rs("id") link2 = "DeleteMember.asp?id=" & rs("id")%> <tr> <td><%=rs("id")%></td> <td><%=rs("username")%></td> <td><%=rs("address")%></td> <td><a href="<%=link1%>">Edit</a></td> <td><a href="<%=link2%>">Delete</a></td> </tr> <% rs.movenext loop rs.close destroyConn%> </table> Trang EditMemberForm.asp <!--#include file ="Connection.asp"--> <%'if session("username")="" then response.redirect "LoginForm.html"%> <%id=request.queryString("id") 'validate id openConn set rs = server.createobject("ADODB.Recordset") rs.open "select * from tblUser where id="&id,conn%> <form method="POST" action="EditMemberProcess.asp"> <p>UserName <input type="text" name="username" value="<%=rs("username")%>"></p> <p>Password <input type="password" name="password"></p> <p>Confirm Password <input type="password" name="confirmPassword"></p> <p>Address <input type="text" name="address" value="<%=rs("address")%>"></p>
33
<input type="hidden" name="id" value="<%=id%>"> <p><input type="submit" value="Submit" name="B1"></p> </form> <% rs.close destroyConn%> Trang EditMemberProcess.asp <!--#include file ="Connection.asp"--> <%'if session("username")="" then response.redirect "LoginForm.html"%> <%id=request.form("id") username=request.form("username") password=request.form("password") confirmPassword=request.form("confirmPassword") address=request.form("address") 'validate if username is exist in the tblUsers?,password and confirmPassword are matched?, address openConn sql="UPDATE tblUser SET [username]='" &username& "',[password]='"&password&"',[address]='"&address& "' WHERE id ="&id conn.execute sql destroyConn%> User <%=username%> has been Edited! Trang DeleteMember.asp <!--#include file ="Connection.asp"--> <%'if session("username")="" then response.redirect "LoginForm.html"%> <% openConn id=request.queryString("id") 'validate id conn.execute "Delete from tblUser where id="&id destroyConn %> User has been Deleted!
34
ASP
Hnh 2.5
Hnh 2.6
35
Hnh 2.7 Sau y chng ta xem qua cch lm phn thm sn phm. Cc phn khc lm tng t. Trang AddProductForm.asp <!--#include file ="Connection.asp"--> <%'if session("username")="" then response.redirect "LoginForm.html"%> <% openConn set rs = server.createobject("ADODB.Recordset") rs.open "select * from Category" ,conn %> <form method="POST" action="AddProductProcess.asp"> <p>ProductName <input type="text" name="ProductName"></p> <p>Product Category <select size="1" name="CategoryID"> <%do while not rs.eof%> <option value="<%=rs("CategoryID")%>"> <%=rs("CategoryName")%> </option> <%rs.movenext loop%> </select></p> <p>Price <input type="text" name="price"></p> <p>Description <input type="text" name="description"></p> <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> <% rs.close destroyConn%>
36
ASP
Trang AddProductProcess.asp <!--#include file ="Connection.asp"--> <% CategoryID=request.form("CategoryID") ProductName=request.form("ProductName") Price=request.form("Price") Description=request.form("Description") 'validate openConn sql="insert into Product([ProductName],[CategoryID],[Price],[Description]) values('"&ProductName&"',"&CategoryID&","&Price&",'"&Description&"')" conn.execute sql destroyConn response.write "Successfull Add Product!" %>
Hnh 2.8
37
Gi hng c lu trong 1 bin kiu session theo di qu trnh khch hng mua hng trong phin
Hnh 2.9 Sau y chng ta xem qua cch xy dng mt gi hng bng mng 2 chiu. Gi thit thng tin v Product bao gm (ProductID, ProductName, ProductCategory, Quantity, Price, TotalPrice), v gi hng cha c ti a 15 sn phm. Vy ta c th dng mng 2 chiu kch thc (6,15) m phng gi hng. Mng ny c lu theo kiu bin session c tc dng trong ton phin ca ngi dng. Ta cn thm 1 bin Count m s sn phm hin c trong gi hng. Bin ny cng c kiu session. Cc hm thao tc: AddProductToCart(ProductID): Thm 1 sn phm vo gi hng, nu sn phm c th tng s lng thm 1 UpdateQuantity(ProductID,Quantity): Cp nht s lng ca 1 sn phm trong gi hng RemoveProductFromCart(ProductID): Xa 1 sn phm khi gi hng RemoveAll: Xa rng gi hng ListProduct: Lit k cc mt hng trong gi hng File Global.asa <Script language=VBScript RUNAT=Server> SUB Session_OnStart ReDim arrProduct(6,15) mng 2 chiu m phng gi hng Session("arrProduct")=arrProduct gi hng cha trong session Session("Count")=0 s sn phm hin c trong gi END SUB </Script> ShoppingCart.asp <% 'thm sn phm vo gi hng, nu c th tng s lng ln 1
38
ASP
Sub AddProductToCart(ProductID) arrProduct=Session("ArrProduct") Count=Session("Count") ProductExist=false bin ny dng nh du xem hng c trong gi cha For i=1 to Count if arrProduct(1,i)=ProductID then ProductExist=true hng c trong gi arrProduct(4,i)=arrProduct(4,i)+1 tng s lng ln 1 exit For End if Next If not ProductExist then If Count<15 then Count=Count+1 dng Recordset ly cc thng tin ProductName, CategoryName, Price t DB arrProduct(1,Count)=ProductID arrProduct(2,Count)=ProductName arrProduct(3,Count)=CategoryName arrProduct(4,Count)=1 arrProduct(5,Count)=CLng(Price) arrProduct(6,Count)=0 End if session("ArrProduct")=arrProduct session("Count")=Count end sub Sub RemoveProductFromCart(ProductID) ArrProduct=Session("ArrProduct") Count=Session("Count") ProductExist=false 'xoa san pham trong gio hang
For i=1 to Count if arrProduct(1,i)=ProductID then xa v tr i End if Next If ProductExist then Count=Count-1 For x=1 to 6 xa rng mt hng i arrProduct(x,i)="" Next n=i ProductExist=true exit For
tm thy hng cn
39
while n<15 dn mt hng i+1 v i bt u t mt hng i n cui gi For x=1 to 6 arrProduct(x,n)=ArrProduct(x,n+1) arrProduct(x,n+1)="" Next n=n+1 Wend End if Session("ArrProduct")=ArrProduct Session("Count")=Count end Sub Sub RemoveAll 'xoa tat ca cac mat hang trong gio hang session("ArrProduct")="" session("ArrCount")="" end Sub Sub UpdateQuantity(ProductID,Quantity) cap nhat lai so luong 1 san pham da co trong gio hang ArrProduct=Session("ArrProduct") Count=Session("Count") For i=1 to Count if arrProduct(1,i)=ProductID then arrProduct(4,i)=Quantity exit For End if Next Session("ArrProduct")=ArrProduct Session("Count")=Count end Sub %>
Nht, ting Vit th bng m ny khng ln c th biu din ht s k t cn thit. V vy, ngi ta phi thc hin nhiu gii php khc phc thiu st ny, dn n tnh trng c nhiu bng m khc nhau cng tn ti, thm ch 1 ngn ng cng c nhiu bng m, gy nn s thiu thng nht. Unicode l bng m 2 byte, ra i nhm mc ch xy dng mt b m chun vn nng, thng nht, dng chung cho tt c cc ngn ng trn th gii. B m Unicode gm 16 bit cho mi k t, biu din c 65536 k t. Unicode c th biu din c y cc k t Ting Vit. 2.6.2 M ha UTF-8 Mi k t trong b m Unicode c m ha (encoding) di 1 trong 3 dng: UTF-8 (8 bit), UTF-16 (16 bit) v UTF-32 (32 bit). Trong UTF-8 (Unicode Transfomation Format -8) c s dng ph bin. Mi k t Unicode c m ha UTF-8 s c biu din bng 1 n 4 byte ty thuc vo gi tr m ca k t . V d: trong bng m Unicode ch a c m l 97 (hexa l U+0061) => UTF 32: 0x00000061, UTF-16: 0x0061, UTF-8: 0x61. UTF-8 c s dng ph bin biu din ting Vit theo m Unicode 2.6.3 CodePage v Charset Trong lp trnh ASP, biu din ting Vit ng theo encoding UTF-8, chng ta cn lu 2 im: Hin th ng font UTF-8 trn client (browser) bng cch s dng th <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ( charset gip browser hin th (decode) ng dng d liu c encode) X l ng UTF-8 trn server bng cch t thuc tnh Codepage = 65001. (code page gip server x l d liu ng encoding) <%Response.codepage=65001%> hoc <%Session.codepage=65001%> Session.codepage c thit lp codepage cho ton phin. Cn Response.codepage thit lp codepage cho 1 ln response thi. Thng thng chng ta s dng Session.codepage v nh vy ton b session s c chung 1 codepage thng nht.
Chng ta tun theo nguyn tc sau: S dng UTF-8 charset cho cc trang web S dng th <%session.codepage=65001%> S dng kiu g Unicode trong cc b g (VietKey, Unikey)
41
Hnh 2.10 V d sau minh ha vic thm vo v hin th d liu t database ra mn hnh vi Ting Vit: Trang RegistrationVNmeseForm.html <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form method="POST" action="RegistrationVNmeseProcess.asp"> <p> Username: <input type="text" name="username"></p> <p> Password:<input type="text" name="password"></p> <p> Confirm Password:<input type="text" name="ConfirmPassword"> </p> <p> Address:<input type="text" name="address"></p> <p><input type="submit" value="Submit" name="B1"></p> </form> </body></html> Trang RegistrationVNmeseProcess.asp <%session.codepage=65001%> <!--#include file ="Connection.asp"--> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <%username=request.form("username") password=request.form("password") confirmPassword=request.form("confirmPassword") address=request.form("address") validate some information retrieved from submitted form openConn sql="insert into tblUser([username],[password],[address])
42
ASP
values('"&username&"','"&password&"','"&address&"')" conn.execute sql destroyConn response.write "Successfull Registration!"%> </body></html> Trang ListMemberVNmese.asp <!--#include file ="Connection.asp"--> <head><meta http-equiv="Content-Type" content="text/html; charset=utf8"></head> <%'if session("username")="" then response.redirect "LoginForm.html"%> <%openConn set rs = server.createobject("ADODB.Recordset") rs.open "select * from tblUser", conn%> <table border="1" width="200"> <tr><td>ID</td><td>Username</td><td>Address</td><td>Edit</td><t d>Delete</td></tr> <% do while not rs.EOF link1 = "EditMemberForm.asp?id=" & rs("id") link2 = "DeleteMember.asp?id=" & rs("id")%> <tr><td><%=rs("id")%></td><td><%=rs("username")%></td><td><% =rs("address")%></td><td><a href="<%=link1%>">Edit</a></td><td><a href="<%=link2%>">Delete</a></td> </tr> <% rs.movenext loop rs.close destroyConn%> </table>
43