"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part 2.  Arrays  Functions  Passing Variables in a URL  Passing variables with forms  Sessions.

Similar presentations


Presentation on theme: "Part 2.  Arrays  Functions  Passing Variables in a URL  Passing variables with forms  Sessions."— Presentation transcript:

1 Part 2

2  Arrays  Functions  Passing Variables in a URL  Passing variables with forms  Sessions

3  An array is a set of indexed elements where each has its own, unique identification number.  Ex: <% Dim fruitlist, arrFruits fruitlist = "apples, pears, bananas, oranges, lemons" arrFruits = Split(fruitlist,",") %>

4 <% Dim fruitlist, arrFruits fruitlist = "apples, pears, bananas, oranges, lemons" arrFruits = Split(fruitlist,",") Response.Write" The list of fruits: Response.Write" “ Response.Write" " & arrFruits(0) & " “ Response.Write" " & arrFruits(1) & " " Response.Write" " & arrFruits(2) & " “ Response.Write" " & arrFruits(3) & " “ Response.Write" " & arrFruits(4) & " “ Response.Write " " %>

5  A function process inputs and return an output.  It can be useful if, for example, you have a wide range of data you have processed or if you have calculations or routines in other ways that must be performed many times.  Syntax: ◦ Function Name(list of parameters)  Statement ◦ End Function

6 <% Function AddAll(number1, number2, number3) AddAll = number1 + number2 + number3 End Function Response.Write"123 + 654 + 9 equals" & AddAll(123,654,9) %> <% Function AddAll(number1, number2, number3) AddAll = number1 + number2 + number3 End Function Response.Write"123 + 654 + 9 equals" & AddAll(123,654,9) %>

7

8  Maybe you have wondered why some URLs looks something like this: http://html.net/page.asp?id=1254  Why is there a question mark after the page name? ◦ The answer is that the characters after the question mark are an HTTP query string. ◦ An HTTP query string can contain both variables and their values. ◦ In the example above, the HTTP query string contains a variable named id, with the value 1254.

9 QueryString <% ' Variable name and age is found in the URL Response.Write " Hello " & Request.QueryString("name") & " " Response.Write " You are " & Request.QueryString ("age") & " years old “ %>

10  When you code a form, there are two particular important attributes: ◦ Action ◦ Method

11  When you code a form, there are two particular important attributes: ◦ Action  Is used to enter the URL where the form is submitted.  In this case it would be the ASP file that you want to handle the input. ◦ Method

12  When you code a form, there are two particular important attributes: ◦ Action ◦ Method  Can either have the value "post" or "get" which are two different methods to pass data.  Using "get", the data is sent through the URL,  Using "post", the data is sent as a block of data through standard input service (STDIN)

13  The page that contains the form doesn't need to be an ASP file (but it can be) Form Enter your name

14 Form What is your name: What is your favorite color: Red Green Blue Form What is your name: What is your favorite color: Red Green Blue example :http://html.net/tutorials/asp/lesson11_ex2.asp

15

16 <% strHeading = " Hello " & Request.Form("username") & " " Select Case Request.Form ("favoritecolor") Case "r" strBackgroundColor = "rgb(255,0,0)" Case "g" strBackgroundColor = "rgb(0255.0)" Case "b" strBackgroundColor = "rgb(0,0,255)" Case Else strBackgroundColor = "rgb(255,255,255)" End Select %> Form ;"> <% strHeading = " Hello " & Request.Form("username") & " " Select Case Request.Form ("favoritecolor") Case "r" strBackgroundColor = "rgb(255,0,0)" Case "g" strBackgroundColor = "rgb(0255.0)" Case "b" strBackgroundColor = "rgb(0,0,255)" Case Else strBackgroundColor = "rgb(255,255,255)" End Select %> Form ;">

17  Thereby, a session was started.  As described above, each session is given an ID by the server.  Session have a default duration of 20 minutes  Can be changed using the following code:  If you want to stop a session, it can always be killed in this way:

18 Login Username: Password: Login Username: Password: Example: http://html.net/tutorials/asp/lesson12_ex1.asp

19 Login <% ' Check if username and password are correct If Request.Form("username") = "asp" AND Request.Form("password") = "asp" Then ' If correct, we set the session to YES Session("login") = "YES" Session.Timeout = 30 Response.Write " You are now logged in " Response.Write " Link to protected file “ Else 'If not correct, we set the session to NO Session("login") = "NO" Session.Timeout = 30 Response.Write " You are NOT logged in " Response.Write " Link to protected file " End If %> Login <% ' Check if username and password are correct If Request.Form("username") = "asp" AND Request.Form("password") = "asp" Then ' If correct, we set the session to YES Session("login") = "YES" Session.Timeout = 30 Response.Write " You are now logged in " Response.Write " Link to protected file “ Else 'If not correct, we set the session to NO Session("login") = "NO" Session.Timeout = 30 Response.Write " You are NOT logged in " Response.Write " Link to protected file " End If %>

20 <% ' If the user is not logged in ' send him/her to the login form If Session ("login") <>"YES" Then Response.Redirect “form.asp” End If %> Login This document is protected You can only see it if you are logged in. <% ' If the user is not logged in ' send him/her to the login form If Session ("login") <>"YES" Then Response.Redirect “form.asp” End If %> Login This document is protected You can only see it if you are logged in.

21


Download ppt "Part 2.  Arrays  Functions  Passing Variables in a URL  Passing variables with forms  Sessions."

Similar presentations


Ads by Google