Mark Dixon 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.

Slides:



Advertisements
Similar presentations
CTER Orientation Tutorial The Use of the WebBoard.
Advertisements

Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation.
Welcome to Florida International University Online J.O.B.S. Link Applicant Tutorial.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: HTML and Client-side code.
Welcome to the Brookdale Community College Online Employment System Applicant Tutorial.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 16 – Web applications: HTML and Client-side code.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
1 Welcome to the Colgate University Online Employment System Applicant Tutorial.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 19 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Beginning Active Server Pages Barry Sosinsky Valda Hilley Programming.
Welcome to the University of Florida Online Employment System Applicant Tutorial.
Welcome to the Alaska Statewide System Online Employment System Applicant Tutorial.
Welcome to the Southeastern Louisiana University’s Online Employment Site Applicant Tutorial!
JavaScript Form Validation
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Welcome to the University of West Florida Online Employment System Applicant Tutorial.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Mark Dixon Page 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
State Management. What is State management Why State management ViewState QueryString Cookies.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon Page 1 3 – Web applications: Server-side code (JSP)
Week 9 PHP Cookies and Session Introduction to JavaScript.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
CTER Orientation Tutorial The Use of the WebBoard.
Mark Dixon 1 18 – Web applications: Server-side code (ASP.Net)
Mark Dixon Page 1 18 – Web applications: Server-side code (ASP)
Mark Dixon Page 1 18 – Web applications: Server-side code (PhP)
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.
ASP/ASP.NET: Tricks and Tips How to get Microsoft’s Programming Language to work for you By Wade Tripp Park University
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
22/11/ Selection If selection construct.
Darek Sady - Respondus - 3/19/2003 Using Respondus Beginner to Basic By: Darek Sady.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
31/01/ Selection If selection construct.
Mark Dixon Page 1 04 – Conditional Execution. Mark Dixon Page 2 Questions: Expressions a)What is the result of: 10 * Int( ) b)How many functions.
PHP Form Processing * referenced from
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
9 – Web applications: Server-side code (ASP)
21 – Web applications: Server-side code (ASP)
18 – Web applications: Server-side code (PhP)
18 – Web applications: Server-side code (ASP)
17 – Modular Design in ASP.
12 – Passing Data between pages: Forms, Sessions, & Query Strings
Unit 27 - Web Server Scripting
Introduction to TouchDevelop
Presentation transcript:

Mark Dixon 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings

Mark Dixon 2 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in passing data between pages –Highlight modular design techniques, and demonstrate them in ASP Objectives, by end of this week’s sessions, you should be able to: –pass data between pages, using: Self Posting Query Strings Session Variables –use procedures, functions, parameters, and modules (shared VB files) in ASP.Net

Mark Dixon 3 Example: Logon v2 (design) Restrict access to home page

Mark Dixon 4 Example: Logon v2 (code) Sub btnLogon_Click(s As Object, e As EventArgs) Handles btnLogon.ServerClick Dim un As String Dim pw As String 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 Sub Please logon: Logon2.aspx My Home page Welcome to my home page. Home.htm

Mark Dixon 5 Example: Logon (Fixed Problem) View Source – shows client-side script: No server-side code

Mark Dixon 6 Example: Logon (Problem 2) User can type home page url (address) directly (bypassing logon page)

Mark Dixon 7 Solution Need way for: –password page to tell home page –that user logged in OK

Mark Dixon 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

Mark Dixon 9 Dim LogonOK As Boolean Sub btnLogon_Click(s As Object, e As EventArgs) Handles btnLogon.ServerClick Dim un As String Dim pw As String LogonOK = False un = txtUserName.Value pw = txtPassWord.Value If un = "mark" And pw = "soft131" Then LogonOK = True Response.Redirect("home3.aspx") Else msg.innerText = "Login details incorrect." End If End Sub Please logon: Logon3.aspx Example: Logon v3 (code) Dim LogonOK As Boolean Sub Page_Load() If LogonOK = False Then Response.Redirect("Logon3.aspx") End If End Sub My Home page Welcome to my home page. Home3.aspx  Does not work: always redirect to logon  Variables do not persist between pages LogonOKTrue

Mark Dixon 10 Example: Logon v3 (Error) Variables – don't persist between pages

Mark Dixon 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

Mark Dixon 12 Maintaining State: Session Object Sub btnSend_Click(s As Object, e As EventArgs) Handles btnSend.ServerClick Session("MSG") = "Meet in BGB202" End Sub Sub btnClear_Click(s As Object, e As EventArgs) Handles btnClear.ServerClick Session.Abandon() End Sub Display Send.aspx Session variable –all variants –no declaration Abandon method –deletes all session variables

Mark Dixon 13 Maintaining State: Session Object Sub Page_Load() parMsg.InnerText = Session("MSG") End Sub Display.aspx read session variable, and display in parMsg

Mark Dixon 14 Example: Message Using Session variable: Sub btnSend_Click(s As Object, e As EventArgs) Handles btnSend.ServerClick Session("MSG") = "Meet in BGB202" End Sub Sub btnClear_Click(s As Object, e As EventArgs) Handles btnClear.ServerClick Session.Abandon() End Sub Display Send.aspx Sub Page_Load() parMsg.InnerText = Session("MSG") End Sub Display.aspx MSGMeet in BGB202

Mark Dixon 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

Mark Dixon 16 Passing Data (temporary) Query Strings –Useful for passing information between pages via links

Mark Dixon 17 Maintaining State: Query Strings Data added to end of URL (address): 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

Mark Dixon 18 Example: Date-Time What background colour do you want for you date information? Yellow Light Blue Menu.aspx Sub Page_Load() Dim colour As String colour = Request.QueryString("Colour") PageBody.Style.Add("background-color", colour) parD.InnerHtml = "The date is " + Now().ToString("dd MMM yyyy") + "." parT.InnerHtml = "The time is " + Now().ToString("HH:mm") + "." End Sub DateTime.aspx

Mark Dixon 19 Reference: Server Object Model Request object: calling web page –QueryString: used to get data from address (?) Response object: web page sent back –Redirect: used to navigate to other page Session object: store data between pages –Abandon: clears session data

Mark Dixon 20 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

Mark Dixon 21 Example: Apples (analysis) SPECIFICATION User Requirements –help young children learn to count from 1 to 10 Software Requirements –Functional: –computer selects number between 1 and 10 –computer displays that number of apples –user (child) types digits –computer compares digits to number of apples –Non-functional should be easy to use and interesting

Mark Dixon 22 Example: Apples v2 (design) Functionality: computer selects number between 1 and 10 computer displays that number of apples user types digits computer compares digits to number of apples

Mark Dixon 23 Data Representation Stored data vs. display –display: seven images of apples (easier for people to understand) –Stored data: 7 (easier to process) Dim numApples As Integer h = "" For a = 1 To numApples h = h & " " Next quest.InnerHtml = h

Mark Dixon 24 Example: Apples v2 (code) Dim n As Long Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim html As String Dim a As Long n = 1 + Int(Rnd() * 9) html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html session("numApples") = n End Sub Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick n = session("numApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If End Sub Apples.aspx Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim html As String Dim a As Long n = 1 + Int(Rnd() * 9) html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html session("numApples") = n End Sub

Mark Dixon 25 Example: Apples v2 (code) Dim n As Long Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim html As String Dim a As Long n = 1 + Int(Rnd() * 9) html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html session("numApples") = n End Sub Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick n = session("numApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If End Sub Apples.aspx Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick n = session("numApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If End Sub

Mark Dixon 26 Problem Solving Strategies bottom-up –Create a detailed solution first –Then look for best solution –refactoring – process of: changing internal design of code, without altering what it does top-down –plan overall design –fill in details in practice mixed – novices favour bot-up, experts top-down

Mark Dixon 27 Example: Apples 2 Difficult to see dependencies for lines far apart Dim n As Long Dim html As String Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim a As Long n = 1 + Int(Rnd() * 9) html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html session("numApples") = n End Sub

Mark Dixon 28 Example: Apples 3 Put dependent lines close together Dim n As Long Dim html As String Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim a As Long n = 1 + Int(Rnd() * 9) session("numApples") = n html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html End Sub

Mark Dixon 29 Example: Apples v3 (design) Functionality: computer selects number between 1 and 10 computer displays that number of apples user types digits computer compares digits to number of apples and displays number of apples typed by user

Mark Dixon 30 Example: Apples v3 (code) Dim n As Long Dim html As String Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick Dim a As Long n = 1 + Int(Rnd() * 9) session("numApples") = n html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html End Sub Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick Dim a As Long n = session("numApples") html = "" For a = 1 To txtAns.Value html = html & " " Next If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = html + "Correct, well done!" Else msg.InnerHtml = html + "Sorry, please try again." End If End Sub Apples.aspx Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick Dim a As Long n = session("numApples") html = "" For a = 1 To txtAns.Value html = html & " " Next If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = html + "Correct, well done!" Else msg.InnerHtml = html + "Sorry, please try again." End If End Sub copy + paste

Mark Dixon 31 Modular Design What do lines do (group summary)? n = 1 + Int(Rnd() * 9) Session("NumApples") = n html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html Pick Num. of Apples Display Question

Mark Dixon 32 Modular Design (top level) Top level reads like English algorithm: Dim n As Long Dim html As String Sub btnStart_Click(s As Object, e As EventArgs) Handles btnStart.ServerClick PickRandomNumberOfApples() DisplayQuestion() End Sub Sub btnCheck_Click(s As Object, e As EventArgs) Handles btnCheck.ServerClick n = session("numApples") DisplayApplesUser() DisplayFeedback() End Sub

Mark Dixon 33 Sub PickRandomNumberOfApples() n = 1 + Int(Rnd() * 9) session("numApples") = n End Sub Sub DisplayQuestion() Dim a As Long html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html End Sub Sub DisplayApplesUser() Dim a As Long html = "" For a = 1 To txtAns.Value html = html & " " Next End Sub Sub DisplayFeedback() If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = html + "Correct, well done!" Else msg.InnerHtml = html + "Sorry, please try again." End If End Sub Modular Design (detail) Procedures contain (hide) detail: Sub DisplayQuestion() Dim a As Long html = "How many apples are there? " For a = 1 To n html = html & " " Next quest.InnerHtml = html End Sub Sub DisplayApplesUser() Dim a As Long msg = "" For a = 1 To txtAns.Value msg = msg & " " Next End Sub

Mark Dixon 34 Problem Solving: 9 dots Join all 9 dots –with 4 straight continuous lines

Mark Dixon 35 Problem Solving Process (Name Split) Problem: a variable exists called n. This contains a person's full name (forename, then surname ). It needs to be split into two separate variables. Dim n As String n = "Ruth Jones" Solution Process: –What do I do to solve this manually (on paper)? –How do I know where the forename ends and the surname begins? –The space is the key: Find the space everything before the space is the forename everything after the space is the surname

Mark Dixon 36 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

Mark Dixon 37 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

Mark Dixon 38 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.

Mark Dixon 39 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 properly. 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 Task 5: Add a score facility. –when the page loads, the score should be 0 –when the answer is correct, the score should increase by 1 –when the score goes over 10, a congratulations message should be shown, and the score reset to 0

Mark Dixon 40 Tutorial Exercise: Apples LEARNING OBJECTIVE: identify dependencies between lines of code refactor code: dependent lines closer refactor code: split into routines (procedures and functions) refactor code: make routines self-contained Task 1: Get the Apples v3 example (from the lecture) working Task 2: Modify your page to keep a score. HINT: Try to identify the routines first, then fill in the code.