You are on page 1of 16

Chng 2

Mt s tin ch trong ASP


Mc tiu
Tm hiu mt s tin ch: Registration Login v Logout Qun l User Qun l Product Shopping cart S dng ting Vit trong ASP

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

Chng 2: Mt s tin ch trong ASP

29

destroyConn response.write "Successful Registration!" %>

2.2 Login v Logout


Trong website c th c nhng ni ch dnh cho cc thnh vin ng k m khng dnh cho khch vng lai, truy cp nhng ni ny buc thnh vin phi ng nhp vo website (login), cc thnh vin login sau c th thot (logout) . Vic ghi nh mt thnh vin login c lu trong mt bin kiu session. Khi thnh vin ny logout chng ta ch vic xa bin session ny. Module ny gm form login, file x l form login, file x l logout, database l table tblUser m t trong module Registration.

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>

Chng 2: Mt s tin ch trong ASP

31

</html> Trang Logout.asp <%session.abandon hy session 'session("username")=" %> <a href="LoginForm.html">Login</a>

2.3 Qun l User


Qun l user bao gm: Lit k danh sch user Thm user Sa user Xa user Phn thm user cng tng t nh module Registration

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>

Chng 2: Mt s tin ch trong ASP

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

2.4 Qun l Product


Qun l Product bao gm: Lit k, thm sa xa loi sn phm (Category) Lit k, thm, sa xa sn phm (Product) Phn qun l Category cng tng t nh qun l User Ring phn qun l Product cn lu mi product thuc 1 category no .

Hnh 2.6

Chng 2: Mt s tin ch trong ASP

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

2.5 Shopping cart


Trong cc website shopping online, ta thng dng mt cu trc d liu lu tr nhng hng ha m ngi dng chn mua trong phin ca h, gi l gi hng (tng t nh gi hng khi chng ta i mua hng trong siu th). V d liu, gi hng lu tr danh sch nhng hng ha ngi dng chn mua bao gm nhng thng tin nh ProductID, ProductName, ProductCategory, Quantity, Price, (nhng thng tin ny c trong bng Product v Category trong DB) m phng gi hng, ta c th dng 1 s cu trc nh Dictionary hoc mng 2 chiu.

Chng 2: Mt s tin ch trong ASP

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

Chng 2: Mt s tin ch trong ASP

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 %>

2.6 S dng ting Vit trong ASP


V c bn my tnh ch x l c d liu dng s. Mi k t (character) c my tnh lu tr v x l bng cch nh x chng thnh mt ch s (cn gi l m - code). V d thng thng ch A c m 65, a m 97Bng nh x cc k t thnh cc m di dng s c gi l bng m (character code). Bng m 1 byte: Trong cc bng m 1 byte nh ASCII, mi k t c biu din bng 1 byte. Chng c th biu din ti a 256 k t (k c cc k t hin th c v k t iu khin). Bng m 1 byte ch thch hp vi nhng ngn ng nh ting Anh. i vi cc ngn ng phc tp nh ting Hoa, ting
40 ASP

2.6.1 Bng m Unicode

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.

2.6.4 Lp trnh ting Vit vi ASP:

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)

Chng 2: Mt s tin ch trong ASP

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>

Chng 2: Mt s tin ch trong ASP

43

You might also like