Tuesday, 28 August 2012

ASP Lesson #2 - Forms


An HTML form is a group of text boxes and buttons other things that allow a user to enter data. Here is the form that you see quite often which we will be using:

Username: 
Password: 
Here is the HTML code for the form:

<form method="get" action="showdetails.asp">
Username: <input type="text" name="txtUsername"></input>
Password: <input type="text" name="txtPassword"></input>
<input type="submit" value="Login"></input>
</form>

The method="get" will show the querystring as part of the address in your web browser. The querystring is all the things that come after the ? in an address. Use method="post" to hide the querystring. action=showdetails.asp means that when the Login button is clicked it will go to showdetails.asp where we will process the data from the form.

You can save this form along with all the other code that you need for a basic HTML page as login.htm. The page with the form doesn't have to be an asp page because it doesn't process anything. It is the showdetails.asp page that does all the processing.

Now we will create showdetails.asp. First type the basic code required for an HTML page and then the code that is required for an ASP page. Here is an example:

<% @Language = VBScript %>
<% Option Explicit %>
<html>

<head>
<title>My First ASP Page</title>
</head>

<body>
<%
%>
</body>

</html>

Make sure you save both pages in C:\Inetpub\wwwroot

Requesting form field values

The Request command is used to get the values from the fields of the form. You should first declare variables to store the values that you request. Use the Dim command to declare the variables like this:

Dim Username, Password

If you know Visual Basic then you must remember that you can't choose what data type you want to declare a variable as like you can is Visual Basic.

Next you store the value from the form in the variable using the Request command like this:

Username = Request("txtUsername")
Password = Request("txtPassword")

We must also write the values on the page to make sure it worked using Response.Write.

Response.Write "Your Username is " & Username & "<br>"
Response.Write "Your Password is " & Password

You will see that the & operator has been used to join the string "Your Username is" and the variable Username. You will also see that you can write HTML tags and that they must be between quotes when you it.

Try open http://localhost/login.htm and type something in the form fields and click the login button. Have a look at the result and make sure you understand what we have done.

Functions

There are functions such as the Date function which are built into ASP. Here is an example of how to write the date using the Date function:

Response.Write "The date is " & Date

There are also functions that you can use on data. The Left function for example can be used to get the first letter of the name a person entered.

Dim Name, Surname, Initial
Name = "John"
Surname = "Smith"
Initial = Left(Name, 1)
Response.Write "Hello Mr " & Initial & " " & Surname

There are many more functions available and you can find them on w3schools' list of VBScript functions.

0 comments:

Post a Comment