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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Radio and Selectbox variable value to form field

Status
Not open for further replies.

auger

Technical User
Oct 27, 2001
48
CA
Hello.

I have an html form that I am trying to post to itself. (Objective is to retain the values a user enters in the form fields after clicking submit in case changes are required to only a few fields after initial form completion.)

My text boxes and text areas work fine. I can have the form retain text boxes and textareas by using the following code:


<input name="MyInputName" type="text" id="MyInputID" value="<% = MyVariable01%>"/>

<textarea name="MyOtherInputName" cols="90" id="MyOtherInputID"><% = MyVariable02%>
</textarea>

I can't get radios and select (combo) boxes to return the variable values to the form. Can anyone help?

 
Been a while since I did any ASP but I think a radio box should be accessed as normal as only one value can be selected

'eg
Dim selectedRadio
selectedRadio= Request.Form("myRadio")

The only form item that's problematic is a checkbox / checklist when the user selects multiple items and the form object choices all have the same name.

Dim selectedCheck
selectedCheck = Request.Form("myCheckBoxs")
'returns a comma delimited string of the items selected by the user
Dim arrChecked
arrChecked = Split(selectedCheck , ",")
'now have items in an array

'iterate through to display
Dim index
For index = LBound(arrChecked )to UBound(arrChecked )
Response.Write("<li>" & arrChecked (index ))

Next


Hope this helps


 
For radio buttons and checkboxes use the keyword "checked"

For listboxes use the keyword "selected"

Like this:
Code:
<html>
	<head><title>test</title></head>
	<body>
		<input type='radio' name='r1'> radio
		&nbsp;&nbsp;&nbsp;
		<input type='radio' name='r1' checked > button
		<br><br><br>
		Favorite Variable: 
		<select NAME="FavoriteVariable">
			<option VALUE="1">hoppy</option>
			<option VALUE="2">frood</option>
			<option VALUE="3" selected>foo</option>
			<option VALUE="4">bar</option>
		</select>
	</body>
</html>
 
Thanks guys. Both good responses but I'm still not quite getting the results I'm looking for.

If user selects either of the radios, I'd like the form to show the user's choice after the form has been submitted.

If I use the latter code, the radio set resets itself after submit. If user selected no (for instance) I'd like the form to show 'no' selected after form has been submitted. If user selected 'yes' I'd like the form to show 'yes' after the form has been submitted.

Do you know if that's possible and how I might go about that?

thanks a million

 
All you have to do is look in the Request.Form collection to see what was submitted... the same way you do for a text box.

Like this:
Code:
        <select NAME="FavoriteVariable">
            <option VALUE="1" <% if Request.Form("FavoriteVariable" = "1" then Response.Write "selected" %>>hoppy</option>
            <option VALUE="2" <% if Request.Form("FavoriteVariable" = "1" then Response.Write "selected" %>>frood</option>
            <option VALUE="3" <% if Request.Form("FavoriteVariable" = "1" then Response.Write "selected" %>>foo</option>
            <option VALUE="4" <% if Request.Form("FavoriteVariable" = "1" then Response.Write "selected" %>>bar</option>
        </select>

Obviously if you have more than just a few items in your dropdown list it would be best to put this into a loop.

You do the same thing for a radio button except use "checked" instead of "selected
 
I hve a similar problem.

I have this html on my client:

<A>1. How Long does it take the earth to orbit the sun?</A><BR>
<INPUT type="radio" name="radiobutton1" value="365">A) 365 days<BR>
<INPUT type="radio" name="radiobutton1" value="364">B) 364 days Mth<BR>
<INPUT type="radio" name="radiobutton1" value="300">C) 300 days<BR>
<INPUT type="radio" name="radiobutton1" value="500">D) 500 days<BR>
<BR>


And I have this ASP on my server.

Dim radioB1, radioB2, radioB3, radioB4, radioB5
Dim questCount
questCount = 0

radioB1 = request.form("radiobutton1")

if radioB1 = "365" then
questCount = questCount + 1
end if

'Write out the result
Response.Write("<b>You answered " & questCount & " Questions Correct </b>" & "<br>")


the problem is that questCount is always 0.
How can I get the value of the radio button clicked?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top