The most important thing about ASP is ability to gather information. This Lesson covers the
Get and Post Methods.
Post
Enter the following HTML Code and save it as Post.html:
<html>
<head>
<title>Post.html</title>
</head>
<body bgcolor="cornflowerblue">
<h1>Form Post Methods</h1>
<br>
<form method="post" action="Post.asp">
Please Enter Your Name: <input type="text" name="Name">
<br><br>
<input type="submit" value=" Send ">
</form>
</body>
</html>
Now enter the following ASP Code and save it as Post.asp:
<%
Dim Mname
Mname = Request.Form( "Name" )
%>
<html>
<head>
<title>Post.asp</title>
</head>
<body bgcolor="cornflowerblue">
<h1>Welcome <%=Mname%> to my Web Site.</h1>
</body>
</html>
Run the Post.html program using http://localhost/post.html. Fill in the textbox and
click the Send button. The Post.asp program will say
Welcome YourName to my Web Site.
Note: In the Post.html program the <form> tag has method="post"
this method is the best for processing form data, there is no limit to the amount of data
and the data is held in a buffer were it can not be viewed by the user. The <form>
tag also has action="post.asp" this means when the Send button is clicked, the Post.asp
program will be called.
The Post.asp program is a combonation of both ASP and HTML. The first part is ASP ( <% ... %> )
In the first line the program declaires Mname as variable (Dim Mname) Next it gives Mname
a value by setting it equal to Request.Form( "Name" ). This line gets the information that was stored
in the buffer by the Post.html program. This information is stored in the buffer under the variable name
Name. We know this because in the line <input type="text" name="Name"> we named the inputbox
Name ( name="Name" ) Now that we have the information stored in Mname we can begin HTML.
The HTML portion is straight HTML except for one little bit in the <h1> tag. After the word Welcome we have
<%=Mname%> this is a concise way to insert an ASP variable in to HTML. As with all variables, it will insert
the variable's value, not the variable's name.
Get
Enter the following HTML Code and save it as Get.html:
<html>
<head>
<title>Get.html</title>
</head>
<body bgcolor="cornflowerblue">
<h1>Form Post Methods</h1>
<br>
<form method="get" action="Get.asp">
Please Enter Your Name: <input type="text" name="Name">
<br><br>
<input type="submit" value=" Send ">
</form>
</body>
</html>
Now enter the following ASP Code and save it as Get.asp:
<%
Dim Mname
Mname = Request.QueryString( "Name" )
%>
<html>
<head>
<title>Get.asp</title>
</head>
<body bgcolor="cornflowerblue">
<h1>Welcome <%=Mname%> to my Web Site.</h1>
</body>
</html>
After running the programmes, look at the Address line in your Browser, do you see your variable and its value?
The Method="get" places the form data in to the URL for the next page. This is a quick
and universal way to pass data from one page to another, but it has some draw backs. One there
is a limit to the amount of data that can be stored in a URL. A URL can not exceed 240 characters (that's the URL plus the data!)
And secondly, the data is easily viewable to any one looking at the browser. Not a place for
confidential information!
Get.asp retrieves the "Name" from the URL with Request.QueryString( "Name" ).
Remove everything (except the div / title) from between <section> and </section>
Place this there instead:
<%
SQL = "Select * From [tblInventory] "
SQL = SQL & "Order by [Item_Name]"
RS.Open SQL
Do Until RS.EOF
Response.Write RS("Item_Name") & "<br>"
RS.MoveNext
Loop
RS.Close
%>
Save and view the Products page. Fix any error before proceeding (you should see a list of the three products.)
Contact me if you are having any problems with this code.
This improved version displays all the fields in the table, uses an HTML table to organize the display, and includes
a button to add the product to a shopping cart. Click the button. These is no Add2Cart.asp page but look at the
URL. Notice how it includes the rcrd_ID (Item=) of the product you clicked on. We will use this information
to determine which product the user want to add to their cart.