• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

Populate multiple checkboxes using loop inside <input> tag using ASP

Hello,

I have a user access form to allow administrators to grant or deny access to a specific function in the website. I have created a table in the database to hold all of the pages in the site (pageid, pagename, category), a table that stores the groups of users (groupid, group) and a table that links the two (id, groupid, pageid).

On my webpage I loop through the categories and pages and group them by category and dynamically write a checkbox for each page in the recordset.

Category 1
          Page 1
          Page 2
Category 2
          Page 3

This part works fine. The part I have having trouble with is dynamically checking or not checking the box based on the records returned from the database. It is only checking the first record in the recordset. My guess is that I need to create a loop inside the <input> tag.

I have two recordsets rsModcat returns all the pages & categories, to display all choices posible to the admin
rsGroup returns the details of the specific group being edited (ID, Name & PageID) There is a record for each page the group is linked to.

Here is the rest of the code:

<%
dim strCat
strCat = ""
                              
Do While Not rsModcat.EOF
     If strCat <> (rsModcat.Fields.Item("Category").Value) Then
          strCat = (rsModcat.Fields.Item("Category").Value)
          Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
          Response.Write("<tr>")
          Response.Write("<td width='4%'>&nbsp;</td>")
          Response.Write("<td width='96%'><font class='greytextbold'>" & strCat & "</font></td>")
          Response.Write("</tr>")
          Response.Write("</table>")                                  
     End If                                    

     Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
     Response.Write("<tr>")
     Response.Write("<td width='14%'><div align='right'>")%>

     <input <%If Not IsNull((rsGroup.Fields.Item("PageID").Value)) Then If Not IsNull((rsModcat.Fields.Item("PageID").Value)) Then If (CStr((rsGroup.Fields.Item("PageID").Value)) = CStr((rsModcat.Fields.Item("PageID").Value))) Then Response.Write("checked") : Response.Write("")%> name='pageid' type='checkbox' id='pageid' value='<%If Not IsNull((rsModcat.Fields.Item("Category").Value)) Then Response.Write(rsModcat.Fields.Item("Category").Value)%>'>
     
     <%Response.Write("</div></td>")
     Response.Write("<td width='86%'><font class='greytext'>" & (rsModcat.Fields.Item("WebPage").Value) & "</font></td>")
     Response.Write("</tr>")
     Response.Write("</table>")      

     rsModcat.MoveNext
Loop%>

Is it possible to loop inside a checkbox? Please help.


Thank you

Aaron









0
vestamedia
Asked:
vestamedia
  • 6
  • 4
  • 2
  • +1
1 Solution
 
Ayesha_KCommented:
haven't understood properly ... your check boxes will be created against every category ??? or every page ???

in following code i tried to make the checked part look a bit simpler and added PAGEID with the checkbox name so that each checkbox has different ID.

try if it works ... if not ... then explain what are the checkboxes for ... and where would they be ..
___________________________________________________________

<%
dim strCat
strCat = ""
dim CheckedVar
Do While Not rsModcat.EOF
     CheckedVar = ""             
     If strCat <> (rsModcat.Fields.Item("Category").Value) Then
          strCat = (rsModcat.Fields.Item("Category").Value)
          Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
          Response.Write("<tr>")
          Response.Write("<td width='4%'>&nbsp;</td>")
          Response.Write("<td width='96%'><font class='greytextbold'>" & strCat & "</font></td>")
          Response.Write("</tr>")
          Response.Write("</table>")                                
     End If                              

     Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
     Response.Write("<tr>")
     Response.Write("<td width='14%'><div align='right'>")
     
     If len(rsGroup.Fields.Item("PageID").Value) > 0 and len(rsModcat.Fields.Item("PageID").Value) > 0 Then
           If (CStr((rsGroup.Fields.Item("PageID").Value)) = CStr((rsModcat.Fields.Item("PageID").Value))) Then
               CheckedVar = "Checked"
           end if
      end if

     <input  <%=CheckedVar%> name='pageid<%=rsGroup.Fields.Item("PageID").Value%>' type='checkbox' id='pageid' value='<%If Not IsNull((rsModcat.Fields.Item("Category").Value)) Then Response.Write(rsModcat.Fields.Item("Category").Value)%>'>
     
     <%Response.Write("</div></td>")
     Response.Write("<td width='86%'><font class='greytext'>" & (rsModcat.Fields.Item("WebPage").Value) & "</font></td>")
     Response.Write("</tr>")
     Response.Write("</table>")    

     rsModcat.MoveNext
Loop%>
0
 
jhleeshpCommented:
Hi,

Don't know if this is what you want. Anyway, there's few ways to do it :

[ONE]
You can write your check box as only
<input name='pageid' <%=strChk%> type='checkbox' id='pageid' value='<%=strValue%>'>
where the 2 variables (strChk and strValue) you can declare as below. E.g.
...
Response.Write("<td width='14%'><div align='right'>")%>

<input name='pageid' <%=strChk%> type='checkbox' id='pageid' value='<%=strValue%>'>

If Not IsNull((rsGroup.Fields.Item("PageID").Value)) and Not IsNull((rsModcat.Fields.Item("PageID").Value)) and (CStr((rsGroup.Fields.Item("PageID").Value)) = CStr((rsModcat.Fields.Item("PageID").Value))) then
      strChk = "checked"
else
      strChk = ""
end if


If Not IsNull((rsModcat.Fields.Item("Category").Value)) Then
      strValue = rsModcat.Fields.Item("Category").Value
end if

<%Response.Write("</div></td>")
...

Think this looks more cleaner and easier to see/debug.


[TWO]
If you also need to use this somewhere else in the script, then it's best to write it as a function like:

<input name='pageid' <%fnChk(CStr((rsGroup.Fields.Item("PageID").Value), CStr((rsModcat.Fields.Item("PageID").Value))%> type='checkbox' id='pageid' value='<%fnVal(rsModcat.Fields.Item("Category").Value)%>'>

Again, with function declared, it's easier to view and to debug if any :)





0
 
vestamediaAuthor Commented:
Thank you both very much for responding so quickly and I apolagize if I am not explaining this well, I think I have been working on the problem for too long.
The code you both provided seems to have moved me in a better direction, instead of only one checkbox being checked (first row in recordset), now several are checked but some are incorrect. From what I can tell it checks the first record from rsGroup and then checks each page after this in acending order from rsModcat.

To better define what I am trying to accomplish, the purpose is to create a checkbox for each page in rsModcat (search, users, forms etc...) Then allow an administrator to edit a group and select the pages that the group can view by checking the box next to the page. If the user is editing a group I need the boxes checked to show the pages they currently have access to. From here I will run update queries to the database with the changes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ayesha_KCommented:
can you post your COMPLETE code ???

i can't see where you are looping through rsGroup recordset
0
 
JagCCommented:
YUK - sorry, again it's another piece of code that is on the HTML part of the page and still has database connections open etc etc. Ahem, sorry.

First up a handy hint - value is the default property of a recordset object thingy.
So you don't have to write:
   
    rsModcat.Fields.Item("PageID").Value

You can just write

    rsModcat("PageID")

Makes it a little easier to read.




You can loop wherever you want. Remember ASP is parsed on the server - ultimately, all that happens is it writes text into an html text file wherever you tell it to.

You could have:

<input name="bob"
<%
' 5000 lines of code here to work something out
response.write(" value = " & something)
%>
type="button">

So yes, what you need to do is loop through the pages drawing a checkbox for each, then when you get to each loop through your access recordset to see if they have access - if so check it, if not dont.

Word of advice - DONT USE RECORDSETS. Sorry :) not shouting. Just a warning.

Several reasons:

1 - it means leaving the DB connection open for longer (slight overhead, possible clashes on low spec server, lots of users)
2 - Pain to navigate (they are not random access. You have to mess about with cursors if you want to go back - you can't dip in and out as you want - THIS MAY BE THE PROB YOU ARE HAVING)
3 - It means more ASP code AFTER your interface code - ideally, you open the DB, get what you want, and close it again, with just a few arrays, and variables with the info you need.
4 - I hate em :)

Seriously though, lookup on getrows() - it's fast, it's stable, it helps componentise code (separates it out easier) etc etc.
0
 
vestamediaAuthor Commented:
I am not, that is where I am needing some assistance. I was not sure if I should do a loop inside the checkbox or somewhere else, also how to loop and set the checked status??

Aaron
0
 
vestamediaAuthor Commented:
JagC,

Thank you for the syntax lesson, but that really does nothing to help resolve my issue.
0
 
JagCCommented:
The first bit was a syntax lesson (rather highlighted something you may not have known to make your life easier)

The second bit answered your question: Is it possible to loop inside a checkbox? Yes it is

The third bit was highlighting the limits of recordsets and informing you this may be the problem why only the first is being checked. And informed you of a different method.

Sometimes, in order to fix things you have to break them.

0
 
vestamediaAuthor Commented:
Thank you.
0
 
Ayesha_KCommented:
vestamedia ... can you please paste your code here ??? the code of the complete page where the recordset are opening and all ..

0
 
vestamediaAuthor Commented:
Here is all the code and thanks again for you time on this.


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim strType
strType = "new"
If Request.QueryString("type") <> "" Then
      strType = Request.QueryString("type")
End If
%>
<%
Dim rsGroup__gid
rsGroup__gid = "-1"
If (Request.QueryString("gid") <> "") Then
  rsGroup__gid = Request.QueryString("gid")
End If
%>
<%
Dim rsGroup
Dim rsGroup_numRows

Set rsGroup = Server.CreateObject("ADODB.Recordset")
rsGroup.ActiveConnection = conn
rsGroup.Source = "{call dbo.sGroupEdit(" + Replace(rsGroup__gid, "'", "''") + ")}"
rsGroup.CursorType = 0
rsGroup.CursorLocation = 2
rsGroup.LockType = 1
rsGroup.Open()

rsGroup_numRows = 0
%>
<%
Dim rsModcat
Dim rsModcat_numRows

Set rsModcat = Server.CreateObject("ADODB.Recordset")
rsModcat.ActiveConnection = conn
rsModcat.Source = "{call dbo.sModuleCategories}"
rsModcat.CursorType = 0
rsModcat.CursorLocation = 2
rsModcat.LockType = 1
rsModcat.Open()

rsModcat_numRows = 0
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
rsModcat_numRows = rsModcat_numRows + Repeat1__numRows
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body onLoad="agtTabs('general');">
<table width="500" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10" height="10"><img src="../../common_files/image/box/box_01.gif" width="10" height="10"></td>
    <td background="../../common_files/image/box/box_02.gif"><img src="../../common_files/image/box/spacer.gif" width="1" height="1"></td>
    <td width="10" height="10"><img src="../../common_files/image/box/box_03.gif" width="10" height="10"></td>
  </tr>
  <tr>
    <td background="../../common_files/image/box/box_04.gif"><img src="../../common_files/image/box/spacer.gif" width="8" height="8"></td>
    <td><form action="" method="post" name="frmagt" id="frmagt">
        <table width="480" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="160"><font class="greytextlrgbold">
              <%If Request.QueryString("type") = "edit" Then Response.Write("Edit Agent") Else Response.Write("New Agent") End If%>
              </font></td>
            <td width="264">&nbsp;</td>
            <td width="56"><div align="right"><a href="javascript:window.close();"><font size="1">Close</font></a></div></td>
          </tr>
          <tr>
            <td colspan="3"><hr></td>
          </tr>
          <tr>
            <td colspan="3"><div align="right">
                <input name="mode" type="hidden" id="mode2" value="save">
                <input name="editnew" type="hidden" id="editnew" value="<%If strType = "edit" Then Response.Write("edit") Else Response.Write("new") End If%>">
                <input name="aid" type="hidden" id="aid">
                <input name="modby" type="hidden" id="modby" value="<%= Session("MM_Username") %>">
                <input name="lastmod" type="hidden" id="lastmod" value="<%=Now()%>">
                <input name="save" onMouseOver="this.style.cursor='hand';" title="Save" type="button" class="button" id="save" onClick="checkThisForm();" value="Save" onKeyDown="if(event.keyCode==13) event.keyCode=9;">
                <input name="cancel" onMouseOver="this.style.cursor='hand';" title="Cancel" type="button" class="button" id="cancel" onClick="window.close();" value="Cancel">
              </div></td>
          </tr>
          <tr>
            <td colspan="3"> <table border='0' cellspacing='0' cellpadding='0'>
                <tr>
                  <td class='bgontabbottom' id='generallt'><img id='generallti' name='generalimg' src='../../common_files/image/tab_subtask_stroked_l.gif' width='7' height='21'></td>
                  <td nowrap id='generallnk' class='bgontabbottommid' onClick="javascript:agtTabs('general');" onmouseover="this.style.cursor='hand';"><font class="greytext">General</font></td>
                  <td class='bgontabbottom' id='generalrt'><img  id='generalrti' name='generalrti' src='../../common_files/image/tab_subtask_stroked_r.gif' width='7' height='21'></td>
                  <td width=1 style='border-style:solid;border-width:0 0 1 0;border-color:white white #cccccc white;'><img src='/common_files/image/spacer.gif' width=1 height=1 border=0></td>                          
                  <td class='bgofftabbottom' id='notelt'><img id='notelti' name='accesslti' src='../../common_files/image/tab_subtask_plain_l.gif' width='7' height='21'></td>
                  <td nowrap id='notelnk' class='bgofftabbottommid' onClick="javascript:agtTabs('note');" onmouseover="this.style.cursor='hand';"><font class="greytext">Access</font></td>
                  <td class='bgofftabbottom' id='notert'><img  id='noterti' name='accessrti' src='../../common_files/image/tab_subtask_plain_r.gif' width='7' height='21'></td>
                  <td width=1 style='border-style:solid;border-width:0 0 1 0;border-color:white white #cccccc white;'><img src='/common_files/image/spacer.gif' width=1 height=1 border=0></td>
                  <td width='99%' style='border-style: solid; border-width: 0 0 1 0 ; border-color: white white #cccccc white;'>&nbsp;</td>
                </tr>
              </table>
              <div id="general" style="display='block'">
              <table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor="eeeeee"  style='border-style: solid; border-width: 0 1 1 1; border-color: #cccccc;'>
                  <tr>
                    <td width="18%">&nbsp;</td>
                    <td width="3%">&nbsp;</td>
                    <td width="79%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td><div align="right"><font class="greytext">Group Name</font></div></td>
                    <td>&nbsp;</td>
                    <td><input type="text" name="textfield" class="textboxreq"> </td>
                  </tr>
                  <tr>
                    <td><div align="right"><font class="greytext">Active</font></div></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                    <td><div align="right"><font class="greytext">Description</font></div></td>
                    <td>&nbsp;</td>
                    <td rowspan="16">&nbsp;</td>
                  </tr>
                </table>
              </div>
              <div id="note" style="display='none'">
                <table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor="eeeeee"  style='border-style: solid; border-width: 0 1 1 1; border-color: #cccccc;'>
                  <tr>
                    <td width="86">&nbsp;</td>
                    <td width="15">&nbsp;</td>
                    <td width="377">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="3">
      <div id="maindiv" style="margin: 0 auto; text-align: left; display: none; width:100%; height: 345px; overflow-y:scroll;">
<%
dim strCat
strCat = ""
                              
Do While Not rsModcat.EOF
      If strCat <> (rsModcat.Fields.Item("Category").Value) Then
            strCat = (rsModcat.Fields.Item("Category").Value)
            Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
                              Response.Write("<tr>")
                                Response.Write("<td width='4%'>&nbsp;</td>")
                                Response.Write("<td width='96%'><font class='greytextbold'>" & strCat & "</font></td>")
                              Response.Write("</tr>")
                           Response.Write("</table>")                                  
      End If                                    
                                          
                      Response.Write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
                              Response.Write("<tr>")
                                Response.Write("<td width='14%'><div align='right'>")

If len(rsGroup.Fields.Item("PageID").Value) > 0 and len(rsModcat.Fields.Item("PageID").Value) > 0 Then
           If (CStr((rsGroup.Fields.Item("PageID").Value)) = CStr((rsModcat.Fields.Item("PageID").Value))) Then
               CheckedVar = "Checked"
           end if
      end if

     <input  <%=CheckedVar%> name='pageid<%=rsGroup.Fields.Item("PageID").Value%>' type='checkbox' id='pageid' value='<%If Not IsNull((rsModcat.Fields.Item("Category").Value)) Then Response.Write(rsModcat.Fields.Item("Category").Value)%>'>


     <%Response.Write("</div></td>")
     Response.Write("<td width='86%'><font class='greytext'>" & (rsModcat.Fields.Item("WebPage").Value) & "</font></td>")
     Response.Write("</tr>")
     Response.Write("</table>")      

Loop%>
      </div></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                  </tr>
                </table>
              </div></td>
          </tr>
        </table>
      </form></td>
    <td background="../../common_files/image/box/box_06.gif"><img src="../../common_files/image/box/spacer.gif" width="1" height="1"></td>
  </tr>
  <tr>
    <td><img src="../../common_files/image/box/box_07.gif" width="10" height="10"></td>
    <td background="../../common_files/image/box/box_08.gif"><img src="../../common_files/image/box/spacer.gif" width="1" height="1"></td>
    <td><img src="../../common_files/image/box/box_09.gif" width="10" height="10"></td>
  </tr>
</table>
</body>
</html>
<%
rsGroup.Close()
Set rsGroup = Nothing
%>
<%
rsModcat.Close()
Set rsModcat = Nothing
%>
0
 
vestamediaAuthor Commented:
Ayesha_K

Thank you for your help. We have decided to take a slightly different approch to our problem, with worked without a hitch. I decided to give you the points even though I did not completly solve my problem, you were the most helpful. Thanks again
0
 
Ayesha_KCommented:
thanx vestamedia ..

sorry for not having enough time to look at your code though ... when i came back ... u had already given me the points ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now