Working with forms
In dynamic sites it's very common that the user sends data to the server through a HTML-form.
With ASP this data can for instance be, stored in a database, sent via an email, or used to look-up
other data.
The data in a form can be send to the server in two ways, with the
POST or with the GET method. With
POST the data is sent to the server as a part of the
HTTP headers, with GET as a part of the URL. When using the last
method the data are placed behind a ? that comes after the
part of the URL that designates the file requested, as follows:
http://www.sample.nl/sample.asp?name=John%20Doe&country=Netherlands
The different values are seperated by & characters, and for example
spaces are coded to prevent that an unusable URL is created. The disadvantage of the
GET method is that the data is part of the URL, which especially is
awkward when it concerns a password. Also the length of an URL is limited, for most browsers
up to about 1.000 characters.
Whichever method you use, the values in the form are ALWAYS of the subtype
String. This means that when you send a number this has to be converted
to a number subtype like Integer, Long, Single or Double. You can do this respectively with
the functions CInt(), CLng(),
CSng() and CDbl().
Using data with the POST method
When we use the POST method, the values can be read
from the Form Collection of the Request object with the command
Request.Form("NameOfValue"). Below you can find an example
the HTML form and the code.
form1.asp
<HTML>
<BODY>
<form method="POST" action="form1post.asp">
<table>
<tr>
<td>
Name
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
Land
</td>
<td>
<input type="text" name="country">
</td>
</tr>
</table>
<input type="submit" value="Send">
<form>
</BODY>
</HTML>
form1post.asp
<%
Option Explicit
%>
<HTML>
<BODY>
<%
Response.Write "Hello " & Request.Form("name") & " from " & Request.Form("land")
%>
</BODY>
</HTML>
Using data with the GET method
When we use the GET method, we can read values
from the QueryString Collection of the Request object with the command
Request.QueryString("NameOfValue"). Below you can find a
sample of the HTML form and the code.
form2.asp
<HTML>
<BODY>
<form method="GET" action="form2get.asp">
<table>
<tr>
<td>
Name
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<input type="text" name="country">
</td>
</tr>
</table>
<input type="submit" value="Send">
<form>
</BODY>
</HTML>
form2get.asp
<%
Option Explicit
%>
<HTML>
<BODY>
<%
Response.Write "Hello " & Request.QueryString("name") & " uit " & Request.QueryString("country")
%>
</BODY>
</HTML>
|