Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting

2 Mark Dixon, SoCCE SOFT 131Page 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

3 Mark Dixon, SoCCE SOFT 131Page 3 Example: Logon Restrict access to home page

4 Mark Dixon, SoCCE SOFT 131Page 4 Example: Logon - code (v2) Using Server-side VB Script: Please logon: Logon.htm <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> Sorry, those login details were incorrect. Please try again LoginFail.aspx My Home page Welcome to my home page. Home.htm

5 Mark Dixon, SoCCE SOFT 131Page 5 Problem Want restricted access: However, can type home page directly (bypassing password page)

6 Mark Dixon, SoCCE SOFT 131Page 6 Solution Need way for: –password page to tell home page –that user logged in OK

7 Mark Dixon, SoCCE SOFT 131Page 7 Passing Data between pages Problem –want to protect all pages from unauthorised access –need to store record of successful login Variables –only persist for duration of page

8 Mark Dixon, SoCCE SOFT 131Page 8 Passing Data (persistent) Cookies (not covered in Soft131) –stored on users’ (client) hard drive –persists between sessions Database/file (covered in next lecture) –stored on server hard drive –persists between sessions

9 Mark Dixon, SoCCE SOFT 131Page 9 Passing Data (temporary) Session object –exists for current session –clears if user closes browser –clears after 20 mins of inactivity Forms and Self Posting Query Strings –Useful for passing information between pages

10 Mark Dixon, SoCCE SOFT 131Page 10 Example: Logon - code (v3) Using Session variable: Please logon: Logon.htm <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Session("LoggedIn") = "Yes" Response.Redirect("home.htm") End If %> Sorry, those login details were incorrect. Please try again LoginFail.aspx My Home page <% If Session("LoggedIn") <> "Yes" Then Response.Redirect("Logon.htm") End If %> Welcome to my home page. Home. aspx

11 Mark Dixon, SoCCE SOFT 131Page 11 Maintaining State: Session Object Login <% If Request.Form("txtUserName") = "George" Then Session("LoginOK") = "Yes" Response.Redirect("Home.aspx") Else Session.Abandon If Request.Form("txtUserName") <> "" Then Response.Write("Invalid user name, please try again.") End If %> Please login: Username: Password: Login.aspx Session variable –all strings Abandon method –deletes all session variables Redirect method –redirects browser to specified page

12 Mark Dixon, SoCCE SOFT 131Page 12 Maintaining State: Session Object <% If Session("LoginOK") <> "Yes" Then Response.Redirect("Login.aspx") End If %> Home Page Welcome to my home page. Home.aspx ASP code to check for successful login

13 Mark Dixon, SoCCE SOFT 131Page 13 Maintaining State: Self Posting Form points to self: If any submit button pressed page re-loads Multiply … … Multiply.aspx

14 Mark Dixon, SoCCE SOFT 131Page 14 Example: Multiply Multiply <% Dim tmpRes Dim tmpNum1 Dim tmpNum2 If Request.Form("txtNum1") <> "" And Request.Form("txtNum2") <> "" Then tmpNum1 = CDbl(Request.Form("txtNum1")) tmpNum2 = CDbl(Request.Form("txtNum2")) tmpRes = tmpNum1 * tmpNum2 End If %> > Multiply.aspx Post to Self Only do calc if first load

15 Mark Dixon, SoCCE SOFT 131Page 15 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

16 Mark Dixon, SoCCE SOFT 131Page 16 Example: Date-Time What background colour do you want for you date information? Yellow Light Blue Menu.aspx > The date is. The time is. DateTime.aspx

17 Mark Dixon, SoCCE SOFT 131Page 17 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

18 Mark Dixon, SoCCE SOFT 131Page 18 Example: Apples 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-functional should be easy to use, and interesting

19 Mark Dixon, SoCCE SOFT 131Page 19 Tutorial Exercises Task 1: Get Logon v3 working (from the lecture) Task 2: Get the Multiply example (from the lecture) working Task 3: Get the Date-Time example (from the lecture) working Task 4: Design and code the Apples example


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting."

Similar presentations


Ads by Google