PDA

View Full Version : Dynamic form checkboxes (ASP)


Claralu
08-04-03, 12:44 AM
I`m trying to generate a form with rows from a database and a checkbox, so that when the user checks a checkbox the program executes a SQL statement with all the rows that are checked.

I`m trying the following code to generate the checkboxes.

<% dim i
i=1
while not tabla.EOF %>
<%dim tot1,totval
totval=int(tabla.RecordCount)
tot1=0
%>
<tr>
<td> <% =tabla.Fields("codigo")%> </td>
<td> <% =tabla.Fields("titulo")%> </td>
<td><input type="checkbox" name="cb"&<%= i %> value=tabla.Fields("codigo")>

</td>
</tr>
<% i=i+1
tabla.MoveNext
wend
%>

Then on the asp page that receives the form submit, I`m trying to access the value of the rows with cb&i name to perform the SQL statement with those rows, but I don`t get any results.

I`m new to ASP, please help.:(

Shane
08-04-03, 04:16 PM
Well, for one, this line is has a few problems.


<input type="checkbox" name="cb"&<%= i %> value=tabla.Fields("codigo")>


when this renders to the browser it will look like this:


<input type="checkbox" name="cb"&1 value=>
<input type="checkbox" name="cb"&2 value=>
<input type="checkbox" name="cb"&3 value=>
...etc



So, basically, you're not naming your checkboxes "cb&NUMBER". You are naming them all "cb" but with some random characters between properties.

You are also trying to set ASP server-side code as the value for the checkbox. You need to put the tabla.Fields("codigo") call between the ASP brackets.

If you want to name all of your boxes "cb", then that's fine. You can just loop through the collection and figure out all of the values.

If you are trying to include that number in the name, then you would want to do something like:


<input type="checkbox" name="cb&<%= i %>" value="<%=tabla.Fields("codigo") %>">




Each of your checkboxes will then be named "cb&1", cb&2", cb&3" and so on. If you don't want the ampersand in there, then just take it out. Your checked checkboxes will also have a value now.

BuildHome
08-06-03, 04:28 PM
You also need to add +1 to variable I, like that:


<%
i=1
while not tabla.EOF
i = i+1
%>
...


So you will get:
1
2
3
4
.....

Otherwise your variable will be always 1...

MadDog
08-06-03, 04:37 PM
This would be what you want to use for the code,


<%
Dim i
Dim tot1
Dim totval

i = 1

Do WHILE NOT tabla.EOF
totval = int(tabla.RecordCount)
tot1 = 0
%>
<tr>
<td><% = tabla("codigo") %></td>
<td><% = tabla("titulo") %></td>
<td><input type="checkbox" name="cb&<% = i %>" value="<% = tabla("codigo") %>"></td>
</tr>
<%
i = i + 1
tabla.MoveNext
Loop
%>