Thursday, March 31, 2011

Variable, Subroutine and Function Names

In the next chapter you will be introduced to variables, subroutines and functions. As the programmer you get to choose and assign the names. The names of the variables and functions must follow these simple rules.

  • The first character must be a letter of the alphabet (lowercase or uppercase).
  • The name can contain an underscore “_”, but NOT start with an underscore.
  • You CANNOT use a number as the first character of the name.
  • Names CANNOT contain spaces.
  • Names CANNOT match any of the reserved words.

 The following are examples of valid names:

We recommend that you use descriptive names for your variables and your functions and that you adopt a standard way of naming things. The two formats that are common are; using the underscore to replace spaces, or capitalizing the first letter of complete words after the first word in the name. For example:


Syntax and Conventions

Writing in any language follows rules and conventions. For example, the English language requires that each sentence must contain a subject and verb to be legitimate. You must also capitalize the first letter of a sentence, and end each sentence with punctuation such as a period or question mark. This is the syntax,or grammar of the English language. Each programming language has a similar set of rules commonly referred to as the syntax.

 Vb Script has several rules and conventions for its syntax:


 Vb Script is a case-insensitive language, meaning that the language will treat these words the same: example, Example, EXAMPLE

White Space:

VBScript, like HTML, ignores spaces, tabs that appear in statements. VBScript does, however recognize spaces, tabs, and newlines that are part of a string. We will talk more about strings later in the course.

x=0 is the same as x = 0

All of these examples will produce the same results. It is a good idea to put some spacing in your code to make it easier to read.

You do need a space between a programming command/function and the data it is working on.

Inserting VBScript into an Active Server Page (.asp)

To create an Active Server Page, the file is normally stored with the .asp extension in a directory on a web server that can process Active Server Pages. You can blend VBScript with normal HTML when creating your Active Server Pages. See below:

<%@ Language=VBScript %>
   <META  NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    dim helloString
  helloString = "Hello  World!"
    <P><% Response.Write  helloString % ></P>

In the above helloworld.asp page, the first line instructs the server that the default scripting language used in this page will be VBScript. Internet Information Server (IIS) uses VBScript as the default scripting language if the language directive is omitted from a page.

To turn on the server side VBScript interpreter you use the less than followed by the percent “<%” characters. To turn off the server side VBScript interpreter you use the percent sign followed by the greater than sign “%>”. Both are indicated in bold in the above example.

Wednesday, March 30, 2011

Inserting Client Side VBScript into an HTML Page

VBScript is added to an HTML page using the SCRIPT tag. The script tags should be placed inside the head tag of the document. They can appear anywhere in the document; but must be before the event that is going to trigger them. If an older browser looks at a page containing script tags it will ignore them, as older browsers are written to ignore tags they can't interpret.

VBScript code should also be placed inside an HTML Comment tag set.

E.g.  <!-- code -->

When used with VBScripts the ending comment tag will also start with two slashes REM which is the VBScript code for comment. This tells the VBScript interpreter to ignore that statement.
This is a standard way for adding VBScript to your HTML pages so that it works properly for browsers that are VBScript enabled and those that do not support VBScript.

  <TITLE>Web Page containing  VBScript</TITLE> 
  <!-- hide VBScript code from  browsers that are not VBScript enabled 
  .(VBScript Statements goes here) 
  REM end hiding of VBScript code  --> 

        (HTML document goes here) 


We may also put in a single line of  code attached to an event. Events will be explained later. The general syntax for  this structure is:

<HTML_TAG  Attribute="option" onEvent="VBScript  code statements go 
  here">stuff in between the opening and closing tag</HTML_TAG>

Introduction to VBScript Programming

This section will provide you with the basics of what VBScript is, and why you would use it.

  1. Interpreted programs versus Compiled programs
  2. Why VBScript?
  3. What you can use VBScript for
  4. About VBScript
Interpreted programs versus Compiled programs

Before we start discussing the differences between interpreted and compiled, we have to define the term source code, more commonly referred to as code. The code is the plain text commands that the program is written in. All programming languages start out as source code; it is then either interpreted or compiled. The code that you will create in this course can be considered source code. 

Interpreted programming languages tend to be simpler to program but slower to execute in general. Each time a program is run, it has to be interpreted (interrogated) line by line, based on the flow of execution (you will see later how branches and loops affect the flow of execution). 

Compiled programming languages have a more complex syntax, and require more strict programming practices. With a compiled programming language, you first write the source code, then you feed it to a compiler (a special computer program), which produces an executable binary program. On the Windows platform, the output of the compiler usually ends in the ".exe" file extension. The program that comes out of the compilation process tends to be platform (operating system) specific. The key benefit for the programmer is that no other programmer can look at the source code once it is compiled. The other key factor is that the language used to write the source code becomes irrelevant once it has been compiled.

Visual Basic is a compiled language, whereas VBScript is an interpreted language.

Why Learn VBScript

VBScript is used to create Active Server Pages (ASPs), to create administration scripts for Windows 95/98/NT, to extend or enhance the functionality of the Microsoft Office products (like Word and Excel (macros)). It can also be used as a client side scripting language for Internet Explorer. Netscape does NOT
support VBScript as a client side scripting language.

About VBScript

 VBScript is an interpreted programming language that can be embedded into an HTML web page or used in server side scripting.Client Side Scripting VBScript code is executed/interpreted when an event is triggered. When the code is executed it is interpreted one line at a time. There are a number of events that will trigger the execution of a VBScript, like clicking on a form button, or the completion of a web page loading. Note: Internet Explorer is the only browser that supports VBScript today.

Server Side Scripting

When the web server loads an .asp page from the disk into memory, it automatically knows to interpret the code in this document. Once the code has been interpreted, the resulting HTML page is sent to the browser (client) making the request.

Friday, March 18, 2011

Sending email with classic ASP with out authentication


Dim Emailstr
Emailstr = "<p>Dear "&qname&"</p>"
Emailstr = Emailstr & "<p>Hello</p>"
Emailstr = Emailstr & "<p>Thanks,<br></p>"
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Subject Line"
myMail.From = " <>" ' Change it with your email ID
myMail.To = Eemail
myMail.HTMLBody = Emailstr
set myMail=nothing

Tuesday, March 15, 2011

Classic ASP and AJAX

It’s so much easier than you think to get started using AJAX, you just need a basic knowledge of JavaScript and Classic ASP to build your first AJAX enabled page.
There are 3 parts to every AJAX request.

  • The Request Page - this can be HTML, ASP, ASPX, PHP or any other type of web page you need to work with.
  • JavaScript Functions - these are included in the Request Page, and handle the AJAX request and response.
  • The Server Side Page - this page receives the request on the server, and sends something back to the JavaScript Function
Our simple example will search for the first name and last name from the database and display it on the page. This is a very simple version. All of this will occur without the page reloading, giving us the magical AJAX effect!

The below code will really helped you to understand the link between classic ASP and Ajax

1. The Request Page

This page can be any type of web page, and can provide many ways of launching an AJAX request. For our simple example, we are using two text boxes and onKeyUp() event of JavaScript to show the response.

<script src="ajax.js" language="javascript"></script>
var whichLink = "nameQuery.asp"
var whichElement = "insert_response"</script>
<form name="form1" action="" method="post">
First<input name="first_name" id="first_name" type="text" onKeyUp="showData()"><br>
Last<input name="last_name" id="last_name" type="text" onKeyUp="showData()"><br><br><br>
<div id="insert_response"></div>
2. The Javascript Functions

var xmlHttp
 function showData()
//this shows the "working" graphic when the query page is retrieving data
document.getElementById(whichElement).innerHTML = "<div align=center><b>Working....</b><br><br></div>"

//by setting these values to "" it prevents an undefined error. Also defining these outside of the function will add existing data to the variable which will cause an error.
var theForm = ""
var howManyElement = ""
var daString = ""

theForm = document.form1
//this finds the number of elements on the form and assigns the number to the howManyElement variable.
howManyElement = theForm.elements.length;


//this for block creates the string with all the data in the url. The loop goes through each element and assigns the form name and the value to the string creating a "first=brian&last=collier' string if the form contains a 'first' field and a 'last' field.

for (i=0; i<howManyElement; i++){
 daString = daString + theForm.elements[i].name+ "="+theForm.elements[i].value+"&";

if (xmlHttp==null)
  alert ("Your browser does not support AJAX!");

//the 'whichLink' variable is assigned on the page making the request.
var url=whichLink;

//alert(url)//this is used to view the URL being sent to the query page.

function stateChanged()

if (xmlHttp.readyState==4)


function GetXmlHttpObject()
var xmlHttp=null;
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
catch (e)
  // Internet Explorer
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  catch (e)
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
3. The Server Side

'this creates the dynamic query for the search. This is a very simple version.
first_name = request.QueryString("first_name")
last_name = request.QueryString("last_name")
if first_name <> "" and last_name = "" then
dynQuery = "first_name like '"& first_name&"%'"
elseif first_name = "" and last_name <> "" then
dynQuery = "last_name like '"& last_name&"%'"
elseif first_name <> "" and last_name <> "" then
dynQuery = "last_name like '"& last_name &"%' AND first_name like '"&first_name&"%'"
end if

Dim rs1
Dim conn
dim query
dim Cmd
conn = "dsn=ajax;"
Set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.ActiveConnection = conn
rs1.Source = "select * from names where " & dynQuery
rs1.CursorType = 0
rs1.CursorLocation = 2
rs1.LockType = 1
rs1_numRows = 0
Dim Repeat1__numRows
Dim Repeat1__index
Repeat1__numRows = -1
Repeat1__index = 0
rs1_numRows = rs1_numRows + Repeat1__numRows
While ((Repeat1__numRows <> 0) AND (NOT rs1.EOF))
response.Write(rs1("first_name") & " " & rs1("last_name") & " | " & rs1("email") & "<br><br>")
Set rs1 = Nothing

That's all the coding required, you can download the files for the demo below for the quick solution.

Classic ASP and AJAX

There is a lot of confusion about AJAX and what it is. Mainly, it is a client-side application that sends data to a back end parser, and thus the back-end processor can be written in anything from ASP to JSP, PHP or any other web based language.

First of all, AJAX is a very simple concept. Get it out of your head that it’s complicated......!!!

In the 1990s, most web sites were based on complete HTML pages; each user action required that the page be re-loaded from the server (or a new page loaded). This process is inefficient, as reflected by the user experience: all page content disappears then reappears, etc. Each time a page is reloaded due to a partial change, all of the content must be re-sent instead of only the changed information. This can place additional load on the server and use excessive bandwidth.

Asynchronous loading of content first became practical when Java applets were introduced in the first version of the Java language in 1995. These allow compiled client-side code to load data asynchronously from the web server after a web page is loaded.In 1996, Internet Explorer introduced the iframe element to HTML, which also enabled asynchronous loading. In 1999, Microsoft created the XMLHTTP ActiveX control in Internet Explorer 5, which was later adopted by Mozilla, Safari, Opera and other browsers as the XMLHttpRequest JavaScript object. Microsoft has adopted the native XMLHttpRequest model as of Internet Explorer 7, though the ActiveX version is still supported. The utility of background HTTP requests to the server and asynchronous web technologies remained fairly obscure until it started appearing in full scale online applications such as Outlook Web Access (2000) and Oddpost (2002), and later, Google made a wide deployment of Ajax with Gmail (2004) and Google Maps (2005).

The term Ajax was coined on February 18, 2005 by Jesse James Garrett in an article entitled Ajax: A New Approach to Web Applications.

On April 5, 2006 the World Wide Web Consortium (W3C) released the first draft specification for the XMLHttpRequest object in an attempt to create an official web standard.

The following technologies are incorporated in AJAX:

HTML or XHTML and CSS for presentation.
The Document Object Model (DOM) for dynamic display of and interaction with data
XML for the interchange of data, and XSLT for its manipulation
the XMLHttpRequest object for asynchronous communication
JavaScript to bring these technologies together

Before using any technology, it is better to know its advantages and drawbacks. Here are some advantages and drawbacks of Ajax.

Advantages a) Allows feedback, confirmation and errors messages to be displayed on same page/view.

b) Wider variety of controls e.g. sliders, date pickers, windows, tabs, spinners etc.

c) No installation, just an AJAX enabled browser required

d) Higher immunity to viruses and piracy.

e) Reduced load on server resources as processing is distributes over server and client

f) Lowered application development and deployment costs

g) Reduction in network traffic due to more intelligent client and selective data request


Pages dynamically created using successive Ajax requests do not automatically register themselves with the browser's history engine, so clicking the browser's "back" button may not return the browser to an earlier state of the Ajax-enabled page.

Any user whose browser does not support JavaScript or XMLHttpRequest, or simply has this functionality disabled, will not be able to properly use pages which depend on Ajax. Similarly, devices such as mobile phones, PDAs, and screen readers may not have support for the required technologies.

Ajax-powered interfaces may dramatically increase the number of user-generated requests to web servers and their back-ends (databases, or other). This can lead to longer response times and/or additional hardware needs.

The asynchronous, callback-style of programming required can lead to complex code that is hard to maintain or debug.

Sunday, March 13, 2011

Writing Log in Classic ASP

'Global variable
Dim StrLogFile : StrLogFile = "log.txt"

'call this sub wherever required
sub WriteLog (msg)

Dim fso, fDebug
set fso = Server.CreateObject ("Scripting.FileSystemObject")
set fDebug = fso.OpenTextFile (StrLogFile , 8, True)
fDebug.WriteLine (FormatDateTime (Now) & " - " & msg)
set fDebug = nothing
set fso = nothing

end sub