<% asp %>

21
<% ASP %> <p> Part 2 </p>

Upload: paulos

Post on 21-Mar-2016

46 views

Category:

Documents


0 download

DESCRIPTION

. Part 2 . Outline. Arrays Functions Passing Variables in a URL Passing variables with forms Sessions. Arrays. An array is a set of indexed elements where each has its own, unique identification number. Ex:

TRANSCRIPT

Page 1: <%  ASP  %>

<% ASP %><p> Part 2 </p>

Page 2: <%  ASP  %>

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

Outline

Page 3: <%  ASP  %>

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,",") %>

Arrays

Page 4: <%  ASP  %>

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

Array Ex:

Page 5: <%  ASP  %>

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

Functions

Page 6: <%  ASP  %>

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

Functions Ex1:

Page 7: <%  ASP  %>

Functions Ex2:

Page 8: <%  ASP  %>

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.

Passing Variables in a URL

Page 9: <%  ASP  %>

<html> <head><title>QueryString</title> </head>

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

http://html.net/tutorials/asp/lesson10_ex2.asp?name=Joe&age=24

Page 10: <%  ASP  %>

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

Passing variables with forms

Page 11: <%  ASP  %>

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

Passing variables with forms

Page 12: <%  ASP  %>

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)

Passing variables with forms

Page 13: <%  ASP  %>

The page that contains the form doesn't need to be an ASP file (but it can be)

<html><head><title>Form</title></head>

<body><h1>Enter your name</h1> <form method="post" action="handler.asp"> <input type="text" name="username"><input type="submit"> </form></body></html>

An HTML page with a form

Page 14: <%  ASP  %>

<html> <head><title>Form</title></head><body> <form method="post" action="handler.asp"> <p>What is your name:</p><input type="text" name="username"><p>What is your favorite color: <input type="radio" name="favoritecolor" value="r" /> Red <input type="radio" name="favoritecolor" value="g" /> Green <input type="radio" name="favoritecolor" value="b" /> Blue </p> <input type="submit" value="Submit" /> </form> </body> </html>

User input and conditions

example :http://html.net/tutorials/asp/lesson11_ex2.asp

Page 15: <%  ASP  %>

The Result of the previous Form

Page 16: <%  ASP  %>

<% strHeading = "<h1>Hello " & Request.Form("username") & "</h1>" 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 %> <html><head><title>Form</title> </head> <body style="background: <% = strBackgroundColor %>;"><% Response.Write strHeading %></body></html>

Example

Page 17: <%  ASP  %>

<% Session ("StartTime") = Now %> Thereby, a session was started. As described above, each session is given an ID by the server.

<% Response.Write Session("StartTime") %>

Session have a default duration of 20 minutes Can be changed using the following code:

<% Session.Timeout = 60 %>

If you want to stop a session, it can always be killed in this way:<% Session.Abandon %>

Sessions

Page 18: <%  ASP  %>

<html><head> <title>Login</title></head><body> <form method="post" action="login.asp"><p>Username: <input type="text" name="username" /></p><p>Password: <input type="text" name="password" /></p><p><input type="submit" value="Let me in" /></p> </form></body></html>

Login system with sessions

Example:http://html.net/tutorials/asp/lesson12_ex1.asp

Page 19: <%  ASP  %>

<html><head><title>Login</title> </head>

<body> <% ' 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 YESSession("login") = "YES" Session.Timeout = 30 Response.Write "<h1> You are now logged in</h1>" Response.Write "<p><a href='document.asp'>Link to protected file</a></p>“Else 'If not correct, we set the session to NOSession("login") = "NO" Session.Timeout = 30 Response.Write "<h1>You are NOT logged in</h1>" Response.Write "<p><a href='document.asp'>Link to protected file</a></p>" End If %> </body> </html>

Page 20: <%  ASP  %>

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

<html> <head> <title>Login</title> </head>

<body> <h1>This document is protected</h1> <p>You can only see it if you are logged in.</p>

</body> </html>

Page 21: <%  ASP  %>

<% Thank You %>