Thursday, November 20, 2008

Beginners II

Write Output to a Browser

The response.write command is used to write output to a browser. The following example sends the text "Hello World" to the browser:

<%

response.write("Hello World!")

%>

There is also a shorthand method for the response.write command. The following example also sends the text "Hello World" to the browser:

<%="Hello World!"%>


VBScript

You can use several scripting languages in ASP. However, the default scripting language is VBScript:

<%

response.write("Hello World!")

%>

The example above writes "Hello World!" into the body of the document.


JavaScript

To set JavaScript as the default scripting language for a particular page you must insert a language specification at the top of the page:

<%@ language="javascript"%>

<%

Response.Write("Hello World!")

%>

Note: Unlike VBScript - JavaScript is case sensitive. You will have to write your ASP code with uppercase letters and lowercase letters when the language requires it.


Other Scripting Languages

ASP is shipped with VBScript and JScript (Microsoft's implementation of JavaScript). If you want to script in another language, like PERL, REXX, or Python, you will have to install script engines for them.

Important: Because the scripts are executed on the server, the browser that displays the ASP file does not need to support scripting at all!

ASP Variables


A variable is used to store information.

If the variable is declared outside a procedure it can be changed by any script in the ASP file. If the variable is declared inside a procedure, it is created and destroyed every time the procedure is executed.


Lifetime of Variables

A variable declared outside a procedure can be accessed and changed by any script in the ASP file.

A variable declared inside a procedure is created and destroyed every time the procedure is executed. No scripts outside the procedure can access or change the variable.

To declare variables accessible to more than one ASP file, declare them as session variables or application variables.

Session Variables

Session variables are used to store information about ONE single user, and are available to all pages in one application. Typically information stored in session variables are name, id, and preferences.

Application Variables

Application variables are also available to all pages in one application. Application variables are used to store information about ALL users in a specific application.

ASP Procedures


In ASP you can call a JavaScript procedure from a VBScript and vice versa.


Procedures

The ASP source code can contain procedures and functions:

<%

sub vbproc(num1,num2)

response.write(num1*num2)

end sub

%>

Result: <%call vbproc(3,4)%>

Insert the <%@ language="language" %> line above the tag to write procedures or functions in another scripting language than default:

<%@ language="javascript" %>

<%

function jsproc(num1,num2)

{

Response.Write(num1*num2)

}

%>

Result: <%jsproc(3,4)%>


Differences Between VBScript and JavaScript

When calling a VBScript or a JavaScript procedure from an ASP file written in VBScript, you can use the "call" keyword followed by the procedure name. If a procedure requires parameters, the parameter list must be enclosed in parentheses when using the "call" keyword. If you omit the "call" keyword, the parameter list must not be enclosed in parentheses. If the procedure has no parameters, the parentheses are optional.

When calling a JavaScript or a VBScript procedure from an ASP file written in JavaScript, always use parentheses after the procedure name.

ASP Forms and User Input


The Request.QueryString and Request.Form commands may be used to retrieve information from forms, like user input.



User Input

The Request object may be used to retrieve user information from forms.

Form example:

First Name:


Last Name:



User input can be retrieved in two ways: With Request.QueryString or Request.Form.


Request.QueryString

The Request.QueryString command is used to collect values in a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

If a user typed "Bill" and "Gates" in the form example above, the URL sent to the server would look like this:

http://www.w3schools.com/simpleform.asp?fname=Bill&lname=Gates

Assume that the ASP file "simpleform.asp" contains the following script:

Welcome

<%

response.write(request.querystring("fname"))

response.write(" " & request.querystring("lname"))

%>

The browser will display the following in the body of the document:

Welcome Bill Gates


Request.Form

The Request.Form command is used to collect values in a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

If a user typed "Bill" and "Gates" in the form example above, the URL sent to the server would look like this:

http://www.w3schools.com/simpleform.asp

Assume that the ASP file "simpleform.asp" contains the following script:

Welcome

<%

response.write(request.form("fname"))

response.write(" " & request.form("lname"))

%>

The browser will display the following in the body of the document:

Welcome Bill Gates


Form Validation

User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and you reduce the server load.

You should consider using server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.

ASP Cookies


A cookie is often used to identify a user.


What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.


How to Create a Cookie?

The "Response.Cookies" command is used to create cookies.

Note: The Response.Cookies command must appear BEFORE the tag.

In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:

<%

Response.Cookies("firstname")="Alex"

%>

It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:

<%

Response.Cookies("firstname")="Alex"

Response.Cookies("firstname").Expires=#May 10,2002#

%>


How to Retrieve a Cookie Value?

The "Request.Cookies" command is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:

<%

fname=Request.Cookies("firstname")

response.write("Firstname=" & fname)

%>


Output:

Firstname=Alex


A Cookie with Keys

If a cookie contains a collection of multiple values, we say that the cookie has Keys.

In the example below, we will create a cookie collection named "user". The "user" cookie has Keys that contains information about a user:

<%

Response.Cookies("user")("firstname")="John"

Response.Cookies("user")("lastname")="Smith"

Response.Cookies("user")("country")="Norway"

Response.Cookies("user")("age")="25"

%>


Read all Cookies

Look at the following code:

<%

Response.Cookies("firstname")="Alex"

Response.Cookies("user")("firstname")="John"

Response.Cookies("user")("lastname")="Smith"

Response.Cookies("user")("country")="Norway"

Response.Cookies("user")("age")="25"

%>

Assume that your server has sent all the cookies above to a user.

Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):

<%

dim x,y

for each x in Request.Cookies

response.write("

")

if Request.Cookies(x).HasKeys then

for each y in Request.Cookies(x)

response.write(x & ":" & y & "=" & Request.Cookies(x)(y))

response.write("
")

next

else

Response.Write(x & "=" & Request.Cookies(x) & "
")

end if

response.write "

"

next

%>

Output:

firstname=Alex

user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25


What if a Browser Does NOT Support Cookies?

If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways of doing this:

1. Add parameters to a URL

You can add parameters to a URL:

Go to Welcome Page

And retrieve the values in the "welcome.asp" file like this:

<%

fname=Request.querystring("fname")

lname=Request.querystring("lname")

response.write("

Hello " & fname & " " & lname & "!

")

response.write("

Welcome to my Web site!

")

%>

2. Use a form

You can use a form. The form passes the user input to "welcome.asp" when the user clicks on the Submit button:

First Name:

Last Name:

Retrieve the values in the "welcome.asp" file like this:

<%

fname=Request.form("fname")

lname=Request.form("lname")

response.write("

Hello " & fname & " " & lname & "!

")

response.write("

Welcome to my Web site!

")

%>

No comments:

Your Title