Your First ASP Script
In this tutorial we will create a simple ASP script to convert from one currency to another, and use this example to explain some of the basic methods of programming ASP.
Usually our ASP scripts will need some kind of input from the user to do their job. Most often the input will be captured in a form. When the form is submitted, the data entered will be sent to the page we specified.
So our first task is to create a form which calls our ASP script. In our example, we'll ask the user to enter the amount they want to convert and the exchange rate. View the code for the HTML form in a new window, and we'll go through the code together.
The first important line is:
<form action="convert.asp" method="get">
This tells our browser to send the results to a file called
convert.asp and to send them using the
GET method. When we use the
GET method we can see what the browser is sending to the script because it appears in the address window - excellent for finding out what's going on. (If we wanted to be a bit more secretive, we could use the
POST method instead by replacing the method string in our form code.)
We then have a couple of text form fields called
exchangerate and the all important
submit button to send the form data to our ASP script. Finally we close off our form. The relevant lines follow:
<input type="text" name="amount" value=""> <input type="text" name="exchangerate" value=""> <input type="submit" name="submit1" value="Convert"> </form>
Select the complete HTML page in the other window, copy it, and save it on your server as
currency_converter.html. If you've been following our web server tutorial, the correct directory is probably
Now for the ASP
Great, now we can start writing our ASP script. ASP tends to look quite a lot like HTML but has embedded code to do some processing along the way. This is what makes it pretty easy to learn. Once again,open the source code in another window and I'll take you through it line by line.
<%@ Language=VBScript %>
This tells the script interpreter that this script is written using VBScript. This is usually the default for ASP, but it's good practise to put this line in anyway. The
<% Option Explicit dim amount, rate, value
Option Explicit means that we have to dimension all of our variables before we can use them. This is a good idea because if we accidentally mis-spell something the interpreter will notice and give us a helpful error message - bugs like this are often the hardest to find, so it's good to try and avoid them if we can. Then we dimension our variables using
dim - this allocates some memory for the computer to store values while our script is running.
amount = Request("amount") rate = Request("exchangerate")
The values we entered in our form can be accessed using
Request("FormVariableName"). In these two lines, we store these values in two ASP variables so that we can use them later.
value = amount * rate %>
This is where we do the calculation. Like a lot of other programming languages
"*" means multiply. We also close the script off, telling the interpreter that the next lines are HTML.
<HTML> <HEAD> <TITLE>Currency Converter</TITLE> </HEAD> <BODY> <p> You entered <%=amount%>.<br> The converted amount is <%=value%>. </p> </BODY> </HTML>
This is just straight HTML, but if you look carefully you'll find 2 bits of embedded ASP:
<%=value%>. This is telling the interpreter to substitute the values of those variables in at those points. (The
"=" is important here - if you leave it out, you won't get anything substituted.) And there you have it, your first ASP script. Well done!
This script was pretty simple and there are lots of things you could do to improve it. As an exercise, you might want to try changing the form to convert between pounds and dollars or vice versa, and changing the ASP script to store the exchange rates in variables and then to decide which is the right exchange rate to use. Hint: To do this you will need the
if statement - the syntax is below:
if condition then command1 command2 ... else command1 ... end if
If you manage this OK, you might want to add some error checking to your script. You can use the function
IsNumeric() to check whether the input is valid, and use the
if statement to either write out an error, or to write the result.
Responses to this article
There are no responses yet.
Post a response
Want to add a comment, or ask a question about this article? Post a response.
To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.