PDA

View Full Version : function checks if checkboxes are checked ForEach


jarv
02-16-2010, 04:54 PM
writing a ForEach function to check if a checkbox is checked or not?

a friend gave me this psuedo code but I don't really understand it and which bits to change:

Function IsCategoryInRS(byVal id, ByRef formData)
... for each categoryitem in formData ...
if id == categoryitem. id
IsCategoryInRs = true
...next

End Function

sqlRs = SELECT * FROM Categories where blah = blah

while not sqlRs.EOF

if(IsCategoryInRS(sqlRS("id"), formData) then
exec UPDATE
else
exec INSERT

sqlRs.MoveNext
wend

here is my code that shows the checkboxes

Dim rsCategories1
Set rsCategories1 = Server.CreateObject("ADODB.Recordset")
rsCategories1.ActiveConnection = MM_CON_Database_STRING

rsCategories1.Source = "SELECT tblCategories.ID, tblCategories.CName, (select tblProductCategories.productID from tblProductCategories where tblProductCategories.productID=" & ID & " and tblProductCategories.categoryID=tblCategories.ID) AS pid FROM tblCategories ORDER BY tblCategories.DisplayOrder"
rsCategories1.Open()

%>
<% while NOT rsCategories1.EOF %>

<form>
<input type="checkbox" value="<%=rsCategories1("ID")%>" id="Col_Category" name="Col_Category" style="width:32px;align:left;"
<% if NOT adding then response.write(iif(rsCategories1("pid") <> "","checked",""))%> />
<%=rsCategories1("CName")%><br/>

<%
rsCategories1.movenext
wend
%>
<input type="submit" name="Operation" value="Update">
</form>

Old Pedant
02-16-2010, 07:02 PM
Your friend is way off base.

For starters, *UNCHECKED* checkboxes send NOTHING AT ALL to the server. This is part of the HTML spec; nothing to do with ASP.

If you *need* to have your ASP code aware of *both* checked and unchecked checkboxes, then you have to invent your own scheme to handle them.

I have spent a lot of time answering your questions, on occasion, and I have no idea if you even read my answers, so I'll wait for you to come back and "talk" to me about what you really need to do, rather than writing another long answer that may or may not be relevant to your problem.

jarv
02-17-2010, 09:02 AM
Your friend is way off base.

For starters, *UNCHECKED* checkboxes send NOTHING AT ALL to the server. This is part of the HTML spec; nothing to do with ASP.

If you *need* to have your ASP code aware of *both* checked and unchecked checkboxes, then you have to invent your own scheme to handle them.

I have spent a lot of time answering your questions, on occasion, and I have no idea if you even read my answers, so I'll wait for you to come back and "talk" to me about what you really need to do, rather than writing another long answer that may or may not be relevant to your problem.

no, you misunderstand, it's is suppose to check through a loop if a checkbox is checked
Then it is supposed to UPDATE if it is checked
ELSE INSERT (if it is not already checked)

Old Pedant
02-17-2010, 06:52 PM
Doing the UPDATE if it is checked is easy.

Doing the INSERT if it is *NOT* checked is hard...because you WILL NOT KNOW that it is not checked!! You have to keep track *some other way* of *all* the checkboxes in order to find the ones that aren't checked.

Here, try it yourself in a very very simple ASP page:

<html>
<body>
<%
cbs = Split( Request("chkbox"), ", " )
For c = 0 To UBound(cbs)
Response.Write "The checkbox with value " & cbs(c) & " was checked.<br/>"
Next
%>
<hr>
<form>
<input type="checkbox" name="chkbox" value="Washington"> Washington <br/>
<input type="checkbox" name="chkbox" value="Lincoln"> Lincoln <br/>
<input type="checkbox" name="chkbox" value="Roosevelt"> Roosevelt <br/>
<input type="checkbox" name="chkbox" value="Obama"> Obama <br/>
<input type="checkbox" name="chkbox" value="Ghandi"> Ghandi <br/>
<hr>
Check some of those checkboxes and then
<input type="submit" value="click here" />
</form>
</body>
</html>

TRY IT. This is how you learn how things work. By creating small experiments and observing the results.

*DO NOT* check, say, the "Lincoln" checkbox. Then hit the submit button. WHAT INDICATION do you have that you did not check "Lincoln"???? Any indication at all? I didn't think so.

jarv
02-22-2010, 03:05 PM
Thanks Old Pendant, you are a great help with your test code, I see how to get values from the checkboxes now

I was just checking to see if my code works... still problems