User Tips: Generic Input Forms
This tip comes from Anton B.
Throughout many of my applications, I find myself checking at the top of every page whether the user has input
the value(s) needed for the processing of the page. That is mostly to make sure the user has reached the page
through the designated form and not by browsing his/her history, and to also avoid the page spitting an ODBC
error.
The logic behind it is quite simple, check for the specified QueryString variable, if it is not there then use
Response.Write to give a decent message and an input form to the user... otherwise process the
page. Below is the skeleton code.
If Request.QueryString("CustomerID") = "" Then
'the subroutine that displays the page
Call DisplayInputForm
Else
Call DisplayPage
End If
|
In one of my applications, I found the practice quite time consuming, and realized that most of my pages were
expecting one variable. Being as lazy as I am, I decided to make the process generic throughout the application,
so I added a table that I called PromptForms to my database. Below is the table structure:
Alongside the table goes a generic prompt page that receives the ID of the form to be displayed and constructs
itself accordingly. Here's the code to a skeleton page:
<%
Dim ID
Dim objCommand
Dim objRS
Const adCmdText = &H0001
ID = Request.QueryString("ID")
Set objCommand = Server.CreateObject("ADODB.Command")
Set objRS = Server.CreateObject("ADODB.RecordSet")
With objCommand
.ActiveConnection = DSNName
.CommandType = adCmdText
.CommandText = "SELECT * FROM promptForms " & _
"WHERE promptForms.ID=" & ID & ";"
.Execute
End With
objRS.Open objCommand
%>
<html>
<head>
<title><%=objRS("frmTitle")%></title>
</head>
<body>
<h1><%=objRS("frmTitle")%></h1>
<form method="<%=objRS("frmMethod")%>"
action="<%=objRS("frmAction")%>">
<p><b><%=objRS("frmText")%></b><br>
<input type="text" name="<%=objRS("frmFieldName")%>">
<input class="button" type="submit"
value=" <%=objRS("frmButtonText")%> ">
</form>
</body>
</html>
<%
objRS.Close
Set objRS = Nothing
Set objCommand = Nothing
%>
|
Now whenever I need to check for input values on a page, it's enough to create an entry in my table and call
it from that page. Here's an example of a page called foo.asp expecting a CustomerID
as a QueryString value:
<%
' foo.asp
' PromptID 1
Dim CustomerID = Request.QueryString("CustomerID")
If Len(Trim(CustomerID)) = 0 Then
Response.Redirect("prompt.asp?ID=1")
End If
%>
|
The values in my table would be like:
ID : 1 'autonumber
frmAction : foo.asp
frmMethod : GET
frmTitle : Enter Customer ID
frmText : You have not provided a Customer ID, please enter one and hit the button
frmFieldName : CustomerID
frmButtonText : enter
The page turned out so useful I am even using it as a primary data gathering page rather than a fail safe
system.
Currently the page only displays one Text Box. I will be working on multiple input fields per page and
extending the input types to possibly include Radio Buttons and Check Boxes.
Stay tuned and happy programming.
Happy Programming!