Nathan Sokalski said:
When form data is submitted to an ASP page using the POST method, it is not
visible in the URL, but it is still resubmitted if the user clicks the
Refresh button. This can cause statistical data to be accidentally submitted
multiple times, making the results incorrect. I would like like the user to
be able to click the Refresh button without resubmitting the form data. Is
there some way to "erase" the form data after it is used so that the Refresh
button does not resubmit it? Any help would be appreciated. Thanks.
Nathan --
Not knowing exactly how your code is written, I can only give you some
generic suggestions.
* In the server-side script, you can check
request.servervariables("REQUEST_METHOD") to determine if it's a POST. If
so, execute the processing to perform the computations, otherwise, only send
the results. Example:
select case lcase(request.servervariables("REQUEST_METHOD"))
case "post"
IsPostback = True 'a boolean to keep track of whether it's post
or get
' pull the inputs from the form and do the calculations
' ....
' store the results in a session variable or database you can
send down
case "get"
IsPostback = False
' retrieve the saved results and send them down
case else
' probably a spider crawling so deal with dismissing it
end select
* If you use the preceding approach, you may want to beef it up some by
adding a test in the case "post" to make sure your submit button was used to
submit perform the submit.
* Depending on the size and nature of the results, you can store them to a
scripting.dictionary object (for simple label-value pairs) and stash it in
the session object, or a database or xml file and stash the retrieval string
in the session. Use a separate function you can call from the page
generation code or in the <body> to return the container and generate the
output HTML as you traverse it, or return fully formatted HTML.
<% option explicit %>
<%
response.buffer = true
dim IsPostback, nAmt, nIntRate, nYears
IsPostback = False
nAmt = 0
nIntRate = 0
nYears = 0
with request
if lcase(.servervariables("REQUEST_METHOD")) = "post" then
IsPostback = True
nAmt = clng(.form("Amount"))
nIntRate = clng(.form("Interest"))
nYears = clng(.form("Years"))
If CalcResults(nAmt, nIntRate, nYears) Then
' deal with everything OK
Else
' deal with error
End If
end if
end with
Function CalcResults(byval Amount, byval IntRate, byval Years)
dim blnRetVal, cn, strSQL, i, PmtAmt
blnRetVal = True
' check that the numbers are reasonable
' if not, blnRetVal = False and Exit Function
' open database connection
set cn = createobject("ADODB.Connection")
cn.open 'yaddayadda
strSQL = "INSERT INTO results_table " & _
" (whos_asking, field1, field2) VALUES ("
for i = 1 to Years
' compute the loan payment for each year
PmtAmt = ...
' save the results
cn.execute strSQL & session("ThisUserID") & _
", " & i & ", " & PmtAmt & ")"
' error trapping would set blnRetVal = False
next
cn.close
set cn = nothing
CalcResults = blnRetVal
End Function
Sub RenderOutputAsTable
' example: write a recordset to an HTML table
' assumes nothing will go wrong, always have error handling
dim cn, strSQL, rsResults
strSQL = "SELECT field1, field2 FROM results_table " & _
" WHERE whos_asking = " & session("ThisUserID")
set cn = createobject("ADODB.Connection")
' prep and open the connection
cn.open 'yaddayadda
with rsResults
.cursortype = adOpenStatic
.locktype = adOpenForwardOnly
.activeconnection = cn
.open strSQL
.activeconnection = nothing
cn.close
set cn = nothing
response.write "<table><tr><td>Field_1</td>" & _
"<td>Field_2</td></tr>"
do until .EOF
response.write "<tr><td>" & .fields(0) & "</td>" & _
"<td>" & .fields(1) & "</td></tr>" & vbCrLf
.movenext
loop
response.write "</table>" & vbCrLf
.close
end with
set rsResult = nothing
End Sub
%>
<html>
<body>
<p>surrounding html</p>
<form name="MyForm" method="POST">
Amount: <input type="text" name="Amount" value="<%=nAmt%>">
Int.%: <input type="text" name="Interest" value="<%=nIntRate%>">
Years: <input type="text" name="Years" value="<%=nYears%>">
<% if IsPostback then %>
<p>Results for <%=session("ThisUserID")%><br>
(<%=nAmt%> for <%=nYears%> at <%=nIntRate%>%)</p>
<% RenderOutputAsTable %>
<p align="center">
<input type="submit" name="SubmitButton" value="Submit">
</p>
</form>
</body>
</html>
This is very over-simplified, but should give you an idea of how it would
work.
Mind you, this solution only works for classic ASP. You would have to use a
different strategy for ASP.NET as you can't intermix script and HTML. It
would be somewhat similar as ASP.NET has the postback check built-in, but
all your assignments would be server-side, and you have to be cognizant of
interaction with viewstate. There are some good articles on fiddling with
viewstate up on MSDN that can give some more guidance.
Alan