Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference form values in your ASP Scripts

Form Handling

Reference form values in your ASP Scripts

by  onpnt  Posted    (Edited  )
Form referencing in your ASP scripts is often a hard process but is relatively trivial when you get the basics of how the objects are sent. Below is going to be a set of four form objects and will be submitted via the URL or QueryString. I do want to point out that there is a FAQ that references the Checkbox and Select a bit more. It is
"How to get the results from a group of checkboxes or a select multiple"
faq333-1162

So let's get started with the script you can run on your own to play with. Just follow the comments. This is going to be brief. The idea of me writing the FAQ in this fashion was for you to run the below script and play with it to see how it works.

<%
' below we just check to see if the form was submitted
If Request.QueryString("subButton") <> "" Then

' this declaration is for the select field. Follow through to see why we need this
Dim selSplitArray

' first the checkbox
'as in the HTML form, the checkbox is either on or "" (empty) it's that simple
If Request.QueryString("chk") = "on" Then
Response.Write "Checkbox was checked | The value was "
Response.Write "<span style=""color:red""> " & Request.QueryString("chk") & "</span><br>"
Else
Response.Write "Checkbox was not checked with no value attribute | There is no value to show!" & "<br>"
End If

' For the second checkbox with a value attribute
' we still need to validate a value passed
' never just do a Request.QueryString("chk2")
' or you will get a empty space in the page
' there are other methods but this is sound
If Request.QueryString("chk") <> "" Then
Response.Write "<span style=""color:red""> " & Request.QueryString("chk2") & "</span><br>"
Else
Response.Write "Checkbox was not checked with value attribute | There is no value passed!" & "<br>"
End If


' for the select. Remember the variable? We need to create a array out of that
'selects are sent comma delimited so you need to split on that delimiter
'to get the values alone for processing. I will not go into the function use here (split etc.)
If InStr(Request.QueryString("sel"),",") > 0 Then
' need to split them
selSplitArray = Split(Request.QueryString("sel"),",")
Response.Write "Values selected in the select box where <br>"
Do While x <= Ubound(selSplitArray)
Response.Write "Value "
Response.Write "<span style=""color:red""> " & selSplitArray(x) & "</span><br>"
x = x + 1
Loop
Else
Response.Write "No values passed in the select box!" & "<br>"
End If

' texts are the easiest. Just call it driectly by name reference
If Request.QueryString("txt") <> "" Then
Response.Write "The text input passed the following "
Response.Write "<span style=""color:red""> " & Request.QueryString("txt") & "</span><br>"
Else
Response.Write "Nothing was passed by the text input<br>"
End If

'radio are identical to the checkbox's on or "" (empty)
If Request.QueryString("rad") = "on" Then
Response.Write "Lastely the radio passed something | The value was "
Response.Write "<span style=""color:red""> " & Request.QueryString("rad") & "</span><br>"
Else
Response.Write "radio was not selected | There is no value to show!" & "<br>"
End If

Else
%>
<html>
<body>
How form objects are passed and referenced in your ASP scripts.<br>

<form method="get">
CheckBox!<br>
Checkbox's are sent either ="" or ="on"<br>

<input type="checkbox" name="chk"><br>


CheckBox Part Two!<br>
Checkbox's sent with value attributes<br>

<input type="checkbox" name="chk2" value="my value"><br>


Select <br>
Multiple selected values are sent comma delimited.<br>
eg: one,three,five<br>

<select multiple name="sel">
<option value="one">one
<option value="two">two
<option value="three">three
<option value="four">four
<option value="five">five
</select>
<br>

Text input's are sent as is.<br>
eg: txt="" or txt="value"<br>

<input type="text" name="txt"><br>

Radios are the same as checkbox's<br>
<input type="radio" name="rad"><br>
<br><br>
<input type="submit" value="test to see them" name="subButton">
</form>
<% End If %>
</html>

See it working (demo) at
http://ASPDevGuru.onpntwebdesigns.com/FormPass.asp

I'd like to thank jemminger for contributing on this FAQ.
In the checkbox discussion you will see two techniques. value attribute and no value attribute.
Thanks jemminger for the great tip.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top