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

Coding radio buttons

Status
Not open for further replies.

apple17

Programmer
Jul 5, 2005
46
US
I have a database (currently Access soon to be SQL) with about 200 'yes / no' indicators in it. My current coding method is to 'convert' to radio buttons each time for each field, e.g.:

<td><input type="radio" name="field43of200" value="yes"
<%if rsData("field43of200")
then response.write("checked") : response.write("") %>> Yes</td>
<td><input type="radio" name="field43of200" value="no"
<%if not rsData("field43of200")
then response.write("checked") : response.write("") %>> No</td>

Then to 'convert' the other way (from the radio button or checkbox) on add / update:

UserSQL = UserSQL & "'" & if request.form("field43of200") = 1 then TRUE else FALSE end if & "'"

(I haven't tested, so my syntax may be off.)

Is there a better way to approach this? Seems like an awful lot of coding.

Thanks

 
Why don't you simply use a function that will build the radio button for you and all you have to do is call it and send it the parameters?

As for sending it back for your add/update, you could simply just send it the value of the radio button (True/False) and it should send it back as you expect without having to use the if/then/else statement.

------------------------------------------------------------------------------------------------------------------------
"I am not young enough to know everything."
Oscar Wilde (1854-1900)
 
Perhaps you could use a For Eacht loop with the recordset's .Fields collection.

Code:
<%
Dim oFld
Do While Not rs.EoF
  For Each oFld in rs.Fields
%>
<td>
  <input type="radio" 
         name="<%= oFld.Name %>" 
         value="yes"
         <% If CBool(oFld.Value) Then response.write("checked") %>> 
  Yes
</td>
<td>
  <input type="radio" 
         name="<%= oFld.Name %>" 
         value="yes"
         <% If Not CBool(oFld.Value) Then response.write("checked") %>> 
  No
</td>

<%   
  Next
  rs.MoveNext
Loop
%>
 
Sheco,

I think this will work for getting the values on to the screen, however, the value I get back for the radio button is 'on', which the database catches as an invalid value for a yes/no value. Maybe I can run a loop like you've suggested to translate the values back?
 
Whoops, I see a typo in that code that I posted above... the 2nd radio button should have had a value of No ... as it is they are both "yes"

Another good thing to do is to give each radio button a unique value in its id property, this comes in especially handy if you are doing any client-side validation.

So something like this:
Code:
<%
Dim oFld
Do While Not rs.EoF
  For Each oFld in rs.Fields
%>
<td>
  <input type="radio" 
         name="<%= oFld.Name %>" 
         [highlight]id="RadioYes<%= oFld.Name %>"[/highlight] 
         value="yes"
         <% If CBool(oFld.Value) Then response.write("checked") %>> 
  Yes
</td>
<td>
  <input type="radio" 
         name="<%= oFld.Name %>"
         [highlight]id="RadioNo<%= oFld.Name %>"[/highlight] 
         [highlight]value="no"[/highlight]
         <% If Not CBool(oFld.Value) Then response.write("checked") %>> 
  No
</td>

<%   
  Next
  rs.MoveNext
Loop
%>
[code]
 
As for looping on the other side... on the ASP that processes the submitted form...It would certainly be possible if your fields have predictable names.

In your example you have a field named field43of200

So maybe something like this:
Code:
Dim strName, strValue
For X = 1 to 200
 strName = "field" & CStr(X) & "of200"
 strValue = Request(strName)
 if strValue = "on" then
   strValue = "yes"
 else
   strValue = "no"
 end if

 'Note: Insert put code here to process field now that you
 '      have the field name and value in the proper format.

Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top