16 – passing data between pages: forms, sessions, & query strings

26
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Upload: noah

Post on 06-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

16 – Passing Data between pages: Forms, Sessions, & Query Strings. Session Aims & Objectives. Aims To introduce the fundamental ideas involved in passing data between pages Objectives, by end of this week’s sessions, you should be able to: pass data between pages , using: Self Posting - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 1

16 – Passing Data between pages:Forms, Sessions, & Query Strings

Page 2: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 2

Session Aims & Objectives• Aims

– To introduce the fundamental ideas involved in passing data between pages

• Objectives,by end of this week’s sessions, you should be able to:

– pass data between pages, using:• Self Posting• Query Strings• Session Variables

Page 3: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 3

Example: Logon v2 (design)• Restrict access to

home page

Page 4: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 4

Example: Logon v2 (code)<script language="VB" runat="server"> Sub Page_Load() Dim un As String Dim pw As String If Request.Form("btnLogon") > "" Then un = txtUserName.Value pw = txtPassWord.Value If un = "mark" And pw = "soft131" Then Response.Redirect("home.htm") Else msg.innerText = "Login details incorrect." End If End If End Sub</script>

<html> <head><title></title></head> <body> <form runat="server"> Please logon:<br /> <input id="txtUserName" type="text" runat="server" /><br /> <input id="txtPassWord" type="text" runat="server" /><br /> <input id="btnLogon" type="submit" value="Logon" runat="server" /> <p id="msg" runat="server"></p> </form> </body></html>

Logon.aspx

<html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body></html>

Home.htm

Page 5: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 5

Example: Logon (Fixed Problem)• View Source – shows client-side script:

No server-side code

Page 6: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 6

Example: Logon (Problem 2)• User can type home page url (address)

directly (bypassing logon page)

Page 7: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 7

Solution• Need way for:

– password page to tell home page

– that user logged in OK

Page 8: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 8

Technique: Dead-Drop Variables• 2 Spies wish to pass message between

each other without actually meeting

• Arrange a dead-drop location– one spy leaves message at location– other spy visits location later to pick up

message

• Variables used as dead-drop containers

Page 9: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 9

Example: Logon v3 (code)<script language="VB" runat="server">Dim LogonOK As Boolean Sub Page_Load() Dim un As String Dim pw As String LogonOK = False If Request.Form("btnLogon") > "" Then un = txtUserName.Value pw = txtPassWord.Value If un = "mark" And pw = "soft131" Then LogonOK = True Response.Redirect("home3.htm") Else msg.innerText = "Login details incorrect." End If End If End Sub</script>

<html> <head><title></title></head> <body> <form runat="server"> Please logon:<br /> <input id="txtUserName" type="text" runat="server" /><br /> <input id="txtPassWord" type="text" runat="server" /><br /> <input id="btnLogon" type="submit" value="Logon" runat="server" /> <p id="msg" runat="server"></p> </form> </body></html>

Logon3.aspx

<script runat="server" language="VB">Dim LogonOK As Boolean Sub Page_Load() If LogonOK = False Then Response.Redirect("Logon3.aspx") End If End Sub</script>

<html> <head><title>My Home page</title></head> <body> <p> Welcome to my home page.<br /> <img src="YouAreHere.jpg" /> </p> </body></html>

Home3.aspx

• Error: always redirect to logon

LogonOKTrue

Page 10: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 10

Example: Logon v3 (Error)• Variables – don't persist between pages

Page 11: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 11

Passing Data (temporary)

• Session object– used to pass information between pages:

– exists for current session– persist between pages– clears if user closes browser– clears after 20 mins of inactivity– no need for declaration

Session("Thing") = 91

Put 91 into Thing

Page 12: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 12

Maintaining State: Session Object<script runat="server" language="VB"> Sub Page_Load() If Request.Form("btnSend") > "" Then Session("MSG") = "Meet in BGB202" ElseIf Request.Form("btnClear") > "" Then Session.Abandon() End If End Sub</script>

<html> <head><title></title></head> <body> <form runat="server"> <input id="btnSend" type="submit" value="Send" runat="server" /> <input id="btnClear" type="submit" value="Clear" runat="server" /> <p><a href="Display.aspx">Display</a></p> </form> </body></html>

Send.aspx

• Session variable– all strings– no declaration

• Abandon method– deletes all

session variables

Page 13: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 13

Maintaining State: Session Object

<script runat="server" language="VB"> Sub Page_Load() parMsg.InnerText = Session("MSG") End Sub</script>

<html> <head><title></title></head> <body> <p id="parMsg" runat="server"></p> </body></html>

Display.aspx

• read session variable, and display in parMsg

Page 14: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 14

Example: Message• Using Session variable:

<script runat="server" language="VB"> Sub Page_Load() If Request.Form("btnSend") > "" Then Session("MSG") = "Meet in BGB202" ElseIf Request.Form("btnClear") > "" Then Session.Abandon() End If End Sub</script>

<html> <head><title></title></head> <body> <form runat="server"> <input id="btnSend" type="submit" value="Send" runat="server" /> <input id="btnClear" type="submit" value="Clear" runat="server" /> <p><a href="Display.aspx">Display</a></p> </form> </body></html>

Send.aspx

<script runat="server" language="VB"> Sub Page_Load() parMsg.InnerText = Session("MSG") End Sub</script>

<html> <head><title></title></head> <body> <p id="parMsg" runat="server"></p> </body></html>

Display.aspx

MSGMeet in BGB202

Page 15: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 15

Questions: Session Variables• Write a line of VB code to put the number 74

into a session variable called id.

• Write VB code that displays 'Hello' in parMsg if the session variable called id is equal to 74

Session("id") = 74

If Session("id") = 74 Then

parMsg.InnerText = "Hello"

End If

Page 16: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 16

Example: Apples (analysis)SPECIFICATION

• User Requirements – help children learn numbers 1 - 10

• Software Requirements– Functional:

–display random number of apples (between 1 & 10)–ask child how many apples are there–child enters answer–computer responds appropriately

– Non-functionalshould be easy to use, and interesting

Page 17: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 17

Example: Apples (code)• What will this do?

Dim n As Long

Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html ElseIf Request.Form("btnCheck") > "" Then If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If End If End Sub

Apples.aspx

Page 18: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 18

Passing Data (temporary)

• Query Strings– Useful for passing information between pages

via links

Page 19: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 19

Maintaining State: Query Strings• Data added to end of URL (address):

http://localhost/page.asp?Surname=Bob

• ASP code can use this data:– Request.QueryString("Surname")

• would return the value "Bob"

• Form method=get– data automatically added to query string

Query String

Page 20: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 20

Example: Date-Time<html> <head> </head> <body> <p>What background colour do you want for you date information? <br><a href=DateTime.aspx?Colour=yellow>Yellow</a> <br><a href=DateTime.aspx?Colour=cyan>Light Blue</a> </body></html>

Menu.aspx

<html> <head> </head> <body bgcolor=<%=request.querystring("Colour")%>> <p>The date is <%=Format(Now(), "D")%>. <p>The time is <%=Format(Now(), "T")%>. </body></html>

DateTime.aspx

Page 21: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 21

Reference: Server Object Model• Request object: calling web page

– Form: used to get form data from page– QueryString: used to get data from address (?)

• Response object: web page sent back– Write: used to put text into web page– Redirect: used to navigate to other page– Clear: erases all HTML in web page

• Session object: store data between pages– Abandon: clears session data

Page 22: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 22

Passing Data (persistent)

• Cookies (not covered in this module)– stored on users’ (client) hard drive– persists between sessions

• Database/file (covered in later lectures)– stored on server hard drive– persists between sessions

Page 23: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 23

Tutorial Exercise: Message• LEARNING OBJECTIVE:

pass data between pages using session variables, and (form) self-posting

• Task 1: Get the message example working (from the lecture)

• Task 2: Change the send.aspx page so that when you click the buttons it gives some feedback as to what has happened. hint: add a paragraph

Page 24: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 24

Tutorial Exercise: Apples• LEARNING OBJECTIVE:

pass data between pages using session variables, and (form) self-posting

• Task 1: Type in the code for the Apples example (from the lecture)

• Task 2: Modify this to use a session variable to 'remember' the random number, so that it works.

• Task 3: Change it so that it disables the buttons appropriately

• Task 4: Change it so that it clears the text box and feedback as a new question begins

Page 25: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 25

Tutorial Exercise: Logon• LEARNING OBJECTIVE:

pass data between pages using session variables, and (form) self-posting

• Task 1: Type in the code for the Logon v3 example (from the lecture) NOTE: this will not work properly (variables do not persist between pages)

• Task 2: Modify this to use a session variable to 'remember' whether the logon was successful. Note: It should not be possible to view the source code Note: It should not be possible to bypass the logon

Page 26: 16 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon Page 26

Tutorial Exercise: Date• LEARNING OBJECTIVE:

pass data between pages using query strings

• Task 1: Get the Date-Time example (from the lecture) working

• Task 2: Modify your page to provide another choice of background colour.