RaysWebClass.Com


Lesson 25:   ASP ASP Get/Post Methods


The most important thing about ASP is ability to gather information. This Lesson covers the Get and Post Methods.

  1. 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.

  2. 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" ).

Parent ifame links and variables

To see ifames Click here

Use this code to retreive the parent's URL (from within the iframe):
Request.ServerVariables("HTTP_REFERER")
If you're looking for this string Show=Yes in the parent's URL, use this code:
If InStr(Request.ServerVariables("HTTP_REFERER"),"Show=Yes") > 0 Then
To use a link (from within the iframe) that changes the parent's URL, use this code:
<a href='NewPage.asp' target='_parent'> New page </a>

Web Page Design: Exercise 25


Get Inventory Information From the Databaase
  1. Open myStyles.css in the includes folder
  2. Add this to the bottom:
  3. .item
    {
    border:1px solid black;
    //border-radius: 10px;
    box-shadow: 5px 5px 5px gray;
    }
  4. Open the ASPClass folder
  5. Open the Products.asp page
  6. Remove everything (except the div / title) from between <section> and </section>
  7. 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.

  8. Now switch the above code with this:
    <%
    SQL = "Select * From [tblInventory] "
    SQL = SQL & "Order by [Item_Name]"
    RS.Open SQL
    Response.Write "<table border='0' cellspacing='0' cellpadding='5'"
    Response.Write " style='width:700px;font: 11pt Verdana, Arial;'>"
    Response.Write "<tr><td colspan='5'><hr></td></tr>"
    Do Until RS.EOF
       Response.Write "<tr valign='top'>"
       Response.Write "  <td>"
       Response.Write "  <img src='images/" & RS("Item_Image") & "'"
       Response.Write "   style='border: 1px solid black;'>"
       Response.Write "  </td>"
       Response.Write "  <td><b>" & RS("Item_Name") & "</b></td>"
       Response.Write "  <td>" & RS("Item_Description") & "</td>"
       Response.Write "  <td><b>"
       Response.Write FormatCurrency(RS("Item_Price"))
       Response.Write "  </b></td>"
       Response.Write "  <td>"
       Response.Write "  <input type='button' value='Add to Cart' "
       Response.Write "   onClick='location.href=""Add2Cart.asp?Item=" & RS("rcrd_ID") & """'>"
       Response.Write "  </td>"
       Response.Write "</tr>"
       Response.Write "<tr><td colspan='5'><hr></td></tr>"
       RS.MoveNext
    Loop
    Response.Write ""
    RS.Close
    %>
    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.