| << F.2.5- Common Errors in the Use of Forms | AppendixF | F.3.1- Using RequestSniffer >> |
RequestSniffer.asp
An important lesson to understand about forms is what data looks like as returned from a browser. Once you understand that, it's possible to write script control structures that can react to the user's input. The following code is a "RequestSniffer", which can be used during development to display information about the nature and syntax of the data that ASP receives from a form.
Our sample RequestSniffer is listed below, or it can be downloaded from the Wrox website.
<%@ Language=VBScript %>
<HTML><HEAD>
</HEAD>
<BODY>
<H3>Request Sniffer</H3><hr>
<%
' /////// Decide whether to reap data from QueryString or Form collection
If Request.QueryString <> "" Then
call ShowQueryString
ElseIf Request.Form <> "" then
Call ShowForm
Else
Response.Write "Both the QueryString and Forms collection are empty"
End If
' //////// Call this if data is in Form collection
Sub ShowForm
Response.write "From Form collection - POST<BR>"
Response.Write "Total # of values = "
Response.Write Request.Form.Count & "<BR>"
Response.Write "Entire Form Collection as String: <BR>"
Response.Write Request.Form & "<BR><BR>"
Response.Write "<TABLE BORDER=1><TR>"
For Each MyItem in Request.Form
Response.Write "<TR><TD>"
Response.Write MyItem & "</TD><TD>"
Response.Write Request.Form(MyItem).Item
Response.Write "</TD></TR>"
Next
Response.Write "</TABLE>"
End Sub
' /////// Call this if data is in QueryString collection
Sub ShowQueryString
Response.Write "From QueryString collection - GET<BR>"
Response.Write "Total # of values = "
Response.Write Request.QueryString.Count & "<BR>"
Response.Write "Entire QueryString Collection as String: <BR>"
Response.Write Request.QueryString & "<BR><BR>"
Response.Write "<TABLE BORDER=1><TR>"
For Each MyItem in Request.QueryString
Response.Write "<TR><TD>"
Response.Write MyItem & "</TD><TD>"
Response.Write Request.QueryString(MyItem).Item
Response.Write "</TD></TR>"
Next
Response.Write "</TABLE>"
End sub
%>
</BODY>
</HTML>
This works quite simply. There are three sections, separated by the rows of slashes. The first looks at the QueryString. If it holds anything then we call the procedure called ShowQueryString. If not, then we call the procedure called ShowForm. In this way, the RequestSniffer works equally well for forms that use GET, POST or have no data fields at all.
Within the ShowQueryString or ShowForm subs (they take the same approach), the first line notes which collection is being read. Then the number of items in the collection is printed using the Count property, and the entire QueryString is printed to the page. A table is created, where column one is the data name and column two the data value, with each row being another result from a form field. We build the table with a For Each... loop, which is specifically used for collections; it iterates one loop for each item in the collection without the programmer having to perform tests (like for example a Do…While loop) or know the number of items in the collection (like a simple FOR...NEXT loop).
| << F.2.5- Common Errors in the Use of Forms | AppendixF | F.3.1- Using RequestSniffer >> |

RSS
