Monday, December 27, 2010

The Method Used to Send Data to Server In ASP

When the user clicks the submit button (the button used to collect information from the form and send it), it is assumed that the information on the form is sent to the. HTML allows you to specify how this information would be sent. To support this, the <form> tag is equipped with an attribute called method. This attribute can be assigned one of two values:


  • GET
  • POST


GET: If we are using get method for sending data, the information that is being sent would display in the address bar, starting with a question mark. This would be done as:



<form method="get"> .

The information being carried should not exceed 2048 characters (if we consider that each character uses a byte).

Here is an example:





POST: An alternative is to use post method attribute. In this case, the information is sent directly to the server through a protocol (in this case, HTTP) and would not appear in the address bar of the browser.
If you use POST as the method to send data, the information being carried can be as large as the user/visitor's computer memory (RAM) can afford.

<form action="FileName" method="post">


html>
<head>
<title>Active Server Pages Tutorials - Lesson 3: Simple Data Input/Output</title>
</head>
<body>


<h1>Simple Data Input/Output</h1>


<form action="exercise3.asp" method="post">
<table border="0" width="320">
<tr>
<td width="80">First Name:</td>
<td><input type="text" name="txtFirstName" size="10"></td>
</tr>
<tr>
<td width="80">Last Name:</td>
<td><input type="text" name="txtLastName" size="10">
<input type="submit" value="Submit it">
</td>
</tr>
</table>
</form>


</body>
</html>



A Request From a Form
As mentioned already, after a user has finished preparing values in a form, he or she can send them to you. To allow you to get values from the user, the IIS library is equipped with an object called Request. When a form is created on a web page, that form becomes a property of the Request object.
The objects that are part of the form constitute a collection and each object of the form becomes a member of this collection. To access an object of this collection, you can pass it to the Request.Form property (this can be referred to as an indexed property because each object can be access using its index). For example, you can pass the name of a control, as a string, as argument. If you use the Request.Form property to access a control, the information should be collected using the POST value of the METHOD attribute of the form. Based on this, to access any control on the form, you would type:

Request.Form(Object).OptionalIndex.Count

The object you need to access can be passed as the Object argument. For example, you can pass the name of a form's control as Object, but as a string. This can be done as follows:
Request.Form("txtFirstName")
In future lessons, when we formally learn about arrays and collections, we will review what roles the OptionalIndex factor and the Count value play. For now, we will ignore them.

To calculate the perimeter and area of the square, change the file as follows:


<%@ Language="VBScript" %>
<html>

<head>

<title>Geometry: The Square</title>
</head>

<body>

<h1>Geometric Figures: The Square</h1>
<p>A square is a geometric figure made of 4 equal sides joined at their ends to
form 4 right angles.</p>

<form action="square1.asp" method="post">
  <table border="0" width="316">
    <tr>
      <td width="109">Side:</td>
      <td width="52"><input type="text" name="txtSide" size="14"
        value=<%= Request.Form("txtSide") %> >
      </td>
      <td width="135"><input type="submit" value="Calculate" name="btnCalculate"></td>
    </tr>
    <tr>
      <td width="109">Perimeter:</td>
      <td width="52"><input type="text" name="txtPerimeter" size="14"
        value=<%= Request.Form("txtSide") * 4 %> >
      </td>
      <td width="135"></td>
    </tr>
    <tr>
      <td width="109">Area:</td>
      <td width="52"><input type="text" name="txtArea" size="14"
        value=<%= Request.Form("txtSide") * Request.Form("txtSide") %> >
      </td>
      <td width="135"><input type="reset" value="Reset Form" name="btnReset"></td>
    </tr>
  </table>
</form>

</body>

</html>







No comments:

Post a Comment