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

i give up...radio button loops:(

Status
Not open for further replies.

jason9898

Technical User
May 4, 2005
18
0
0
US
I hate posting a lot of code but i am deperate. I have a database field cal pick_ha, a bit field with 1 and 0 populated. I want to display them in a table and allow users to update them. With this code you can see the dynamic field pick_ha display as true or false when executed but the radio buttons are all marked false...

any ideas?
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/conn.asp" -->
<%
Dim rsSubmitPicks
Dim rsSubmitPicks_numRows

Set rsSubmitPicks = Server.CreateObject("ADODB.Recordset")
rsSubmitPicks.ActiveConnection = MM_conn_STRING
rsSubmitPicks.Source = "SELECT * FROM net.picks_delete WHERE Username = 'Administrator'"
rsSubmitPicks.CursorType = 0
rsSubmitPicks.CursorLocation = 2
rsSubmitPicks.LockType = 1
rsSubmitPicks.Open()

rsSubmitPicks_numRows = 0
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form method="post">
<%

IF rsSubmitPicks.State <> 1 THEN
Response.Write "Data error!"
Response.End
END IF

Do While Not rsSubmitPicks.EOF
%>
ID# <%=(rsSubmitPicks.Fields.Item("GameID").Value)%> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input <%If (CStr((rsSubmitPicks.Fields.Item("Pick_HA").Value)) = CStr("1")) Then Response.Write("CHECKED") : Response.Write("")%> type="radio" name="r<%=(rsSubmitPicks.Fields.Item("GameID").Value)%>" value="1" <%If rsSubmitPicks(1) Then Response.Write " checked "%>>
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input <%If (CStr((rsSubmitPicks.Fields.Item("Pick_HA").Value)) = CStr("0")) Then Response.Write("CHECKED") : Response.Write("")%> type="radio" name="r<%=(rsSubmitPicks.Fields.Item("GameID").Value)%>" value="0" <%If Not rsSubmitPicks(1) Then Response.Write " checked "%>>
False<%=(rsSubmitPicks.Fields.Item("Pick_HA").Value)%> <%=(rsSubmitPicks.Fields.Item("GameID").Value)%><BR>
<%
rsSubmitPicks.MoveNext
Loop



Set adoConn = Nothing
%>
<BR>
<br>
<input type="submit">
</form>
</body>
</html>
<%
rsSubmitPicks.Close()
Set rsSubmitPicks = Nothing
%>
<%


%>
 
jason9898,

It looks like each radio button element has 2 IF/THEN conditionals that can result in the property "checked" ... see red text below:
Code:
<input 
<%[red]If (CStr((rsSubmitPicks.Fields.Item("Pick_HA").Value)) = CStr("1")) Then Response.Write("CHECKED")[/red] : Response.Write("")%> 
type="radio" 
name="r<%=(rsSubmitPicks.Fields.Item("GameID").Value)%>" 
value="1" 
<%[red]If rsSubmitPicks(1) Then Response.Write " checked "[/red]%>
>
 
I think the clue to finding the error will be found if you use the "View Source" feature of your browser to examine the HTML that your ASP sends to the browser.

By studying the HTML, you can see which lines of your ASP accutally executed and also see why the page behaves the way it does in the browser.
 
i do not know how to fix. dreamweaver generates the rado button code and says set value equal to field.

here is the view source

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form method="post">

ID# 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="1" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="1" value="0" checked >
False1 1<BR>

ID# 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="2" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="2" value="0" checked >
False0 2<BR>

ID# 3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="3" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="3" value="0" checked >
False0 3<BR>

ID# 4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="4" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="4" value="0" checked >
False0 4<BR>

ID# 5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="5" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="5" value="0" checked >
False0 5<BR>

ID# 6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="6" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="6" value="0" checked >
False0 6<BR>

ID# 7 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="7" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="7" value="0" checked >
False1 7<BR>

ID# 8 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="8" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="8" value="0" checked >
False1 8<BR>

ID# 9 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="9" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="9" value="0" checked >
False1 9<BR>

ID# 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="10" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="10" value="0" checked >
False1 10<BR>

ID# 11 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="11" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="11" value="0" checked >
False1 11<BR>

ID# 12 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="12" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="12" value="0" checked >
False1 12<BR>

ID# 13 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="13" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="13" value="0" checked >
False1 13<BR>

ID# 14 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="14" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="14" value="0" checked >
False1 14<BR>

ID# 15 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="15" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="15" value="0" checked >
False1 15<BR>

ID# 16 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="16" value="1" checked >
True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="16" value="0" checked >
False1 16<BR>

<BR>
<br>
<input type="submit">
</form>
</body>
</html>
 
Can anyone help me on this? I do not know what do do................
 
Yes, the "View Source" shows the problem quite clearly:

Code:
ID# 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input  type="radio" name="1" value="1"  [highlight]checked[/highlight] > 
  True
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input  type="radio" name="1" value="0"  [highlight]checked[/highlight] > 
  False1                                                 1<BR>

You just need to change the code so that only the True or the False will ever be checked.
 
sheco....I do not know how to do this from the asp side?

please eloborate?
 
Think their has to be some other kind of code missing?
I am clueless?
 
sheco,
This works perfectly with the checkbox just like this below?

%>
<tr>
<td width="8%"><%= Abs((Recordset1.Fields.Item("Pick_HA").Value)) %></td>
<td width="44%"><input <%If (CStr(Abs((Recordset1.Fields.Item("Pick_HA").Value))) = CStr("1")) Then Response.Write("checked") : Response.Write("")%> type="checkbox" name="checkbox" value="1">
<input <%If (CStr(Abs((Recordset1.Fields.Item("Pick_HA").Value))) = CStr("0")) Then Response.Write("checked") : Response.Write("")%> name="checkbox" type="checkbox" id="checkbox" value="0"></td>
<td width="12%">&nbsp;</td>
<td width="12%">&nbsp;</td>
<td width="12%">&nbsp;</td>
<td width="12%">&nbsp;</td>
</tr>
<%


How can I convert that to radio buttons?
 
if i change it to this:
<td width="44%"><input <%If (CStr(Abs((Recordset1.Fields.Item("Pick_HA").Value))) = CStr("1")) Then Response.Write("checked") : Response.Write("")%> type="radio" name="checkbox" value="1">
<input <%If (CStr(Abs((Recordset1.Fields.Item("Pick_HA").Value))) = CStr("0")) Then Response.Write("checked") : Response.Write("")%> name="checkbox" type="radio" id="checkbox" value="0"></td>


It only shows the last record as true? it works fine if it says type = checkbox?

arghh.....
 
I'm kinda confused as to why the recordset was named rsSubmitPicks but now it is named Recordset1.

Anyway, taking your code from the 2 posts above this one and converting to radio buttons will look something like this: (using Recordset1 as the named of the recordset)
Code:
%>
<tr>
  <td width="8%"><%= Abs((Recordset1.Fields.Item("Pick_HA").Value)) %></td>
  <td width="44%">
    <input type="radio" 
           name="r<%= Recordset1("GameID")%>" 
           value="1" 
	   <%If (CStr(Abs(Recordset1("Pick_HA"))) = CStr("1")) Then Response.Write("checked")%> >

    <input type="radio"
           name="r1" 
           value="0" 
           <%If (CStr(Abs(Recordset1("Pick_HA"))) = CStr("0")) Then Response.Write("checked")%> >
  </td>
  <td width="12%">&nbsp;</td>
  <td width="12%">&nbsp;</td>
  <td width="12%">&nbsp;</td>
  <td width="12%">&nbsp;</td>
</tr>
<%
 
The reason why only the last one is checked when you just change the input type from checkbox to raido button is that all of your radio buttons have the same value for their name property.

When multiple checkboxes have the same name, the values are submitted as a comma separated list. Consider this code:
Code:
What do you like for breakfast? (check all that apply)
<input type="checkbox" name="food" value="ham"> Hame
<br>
<input type="checkbox" name="food" value="eggs"> Eggs
<br>
<input type="checkbox" name="food" value="toast"> Toast

If you check all of the boxes, then submitting a form would send an HTTP request to the web server that includes this: food=ham, eggs, toast

The "purpose" of a checkbox is to offer a yes/no question. Checked is yes, unchecked is no. The purpose of a radio button is to give 2 or more mutually exclusive options, like this:
Code:
Which of the following colors do you like the best?
<input type="radio" name="color" value="red"> Red
<br>
<input type="radio" name="color" value="green"> Green
<br>
<input type="radio" name="color" value="blue"> Blue


So, if instead of checkboxes, all of these HTML elements were radio buttons, the behavior would be different. Amoung radio buttons with the same name, only 1 can be checked. Even if you hardcode them as checked, only 1 will be displayed as being checked.
 
I did have to make a slight change to the code and it did indeed work.

Changed the second name= to the same name:
<input type="radio"
name="r<%= Recordset1("GameID")%>"
value="1"
<%If (CStr(Abs(Recordset1("Pick_HA"))) = CStr("1")) Then Response.Write("checked")%> >

<input type="radio"
name="r<%= Recordset1("GameID")%>"
value="0"
<%If (CStr(Abs(Recordset1("Pick_HA"))) = CStr("0")) Then Response.Write("checked")%> >

I was hoping to use DW update feature but it says I need to create a record set first. I was told by a valuable source to reinstall the software and that sometimes fixes it. It let me add it once but then failed.

I guess I am left with learning how to do it straight with code with your help of course sheco...Can you step me through adding the ability to update this data.

As of now it returns what is in the db, I need to provide user with the ability to change the data with a submit button. Is this easy?

again thanks for all your help...
 
The first thing you need is to add to your HTML form the value(s) that uniquely identify the record(s) to be updated.

Looking at the code that you posted above, I will guess that the records to be updated are identified by the value of the fields GameID and Username.

GameID is already in your HTML form because it is part of the name property of your radio buttons... just strip off the "r" and you've got the GameID.

Username will need to be added to the HTML form. The best way to do this is probably a hidden form element. Something like this should do the trick:
<input type="hiddden" name="Username" value="Administrator">

See here it is hard-coded to be Administrator just like the SQL up in the original post is hard coded this way. When you get around to changing that up at the top to handle other users, you gotta come down to this form element and change it the same way, so that the records shown are the ones updated.

Anyway, you also need an action property on your HTML form that points to your new update.asp or whatever you name it.

Four different methods come quickly to mind for actually doing the update but there are other ways also...
1) Updateable ADO recordset.
2) Build SQL and execute with ADO connection object.
3) Build stored procedure and execute with ADO connection object.
4) Build stored procedure and execute with ADO command object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top