Here is a quick way to display the form elements, best for debugging. While this is a very easy way to get all of the elements displayed, they will not be in the order you might expect.
For each x in Request.Form
Response.Write(“<span class=’highlight’>” & x & “</span> = <span class=’myspan’>” & Request.Form(x) & “</span><br />”)
next
This next way first will check if the form has been submitted, and then preserves the original order of the form elements, and is best for coding the form elements into variables or displaying them
If request.form <> “” Then
for x = 1 to Request.Form.count()
Response.Write(Request.Form.key(x) & ” = “)
Response.Write(Request.Form.item(x) & “<br>”)
next
End If
What if you are getting errors when trying to use a varaible that was submitted. There is a good chance that there is whitespace before or after the var. In classic asp you can just use the Trim()
myvar=” ddenney@hotmail.com ” <– those pesky white spaces at the end will mess up the sendEmail function.
MycleanVar=Trim(myvar) — MyCleanVar should now pass through the send email function with out any problems..(assuming you have validated the address).
What if the user has entered their data in “quotes”. Suppose Dan “the man” Rodriques wants to register for sports. The quotes around the name could mess up the sql server insert statement. We just escape the characters with script.
FUNCTION remChars(inString) tempString = Replace(inString,"""","""""") tempString = Replace(tempString,"'","''") remChars = tempString END FUNCTION
Counting Form Submit Variables and using them if they exist.
Now I need to count how many variables received for additional transcripts. They will be passed to me using the following pattern… Other Transcripts1, Other Transcripts2, Other Transcripts3, Other Transcripts4, etc.
After some pondering, I have decided to use a While loop…
amount = 1 While Request.Form("Other Transcripts" & amount) <> "" Response.Write("We have other transcripts (" & amount & "), lets put it into the table." & otherSQL) amount = amount + 1 Wend
This will continue to loop through the variables until it finds them empty.
Removing Harmful Characters from User Input
To protect against vulnerabilities such as script injection and cross-site scripting, user input can be verified and rejected, or an application can simply remove harmful characters and continue processing. This is a summary from the microsoft article
The regular expression, [^A-Za-z0-9_ ], matches any character that is not any of the following:
- An alphabetic character
- A number
- An underscore (_)
- A space
function RemoveBadCharacters(strTemp) { strTemp = strTemp.replace(/[^A-Za-z0-9_ ]/g,""); return strTemp; }