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

Checkbox value 2

Status
Not open for further replies.

JustWondering

Technical User
Jun 28, 2003
57
US
Hi,

I have an SQL update based on the value of the checkboxes:
Code:
SQL="Update Table Set Status = " & CInt(Request("chkStatus")) & " Where ID = " & IntID

But I couldn't get the value of the checkboxes. I always update with checkbox's value = 0.

Here is my checkboxes

Code:
<%Do While NOT RsData.EOF%>
<tr><td>Status<input type=&quot;checkbox&quot; name=&quot;chkStatus&quot; value=&quot;<%Rs(&quot;Status&quot;)%>&quot;>
<%RsData.MoveNext
Loop%>

I guess I'm missing the code to capture the value of the checkboxes. Someone please help!
 
Actually, I use this to update:

for i=1 to request(&quot;chkStatus&quot;).count

SQL=&quot;Update Table Set Status = &quot; & CInt(Request(&quot;chkStatus&quot;)(i)) & &quot; Where ID = &quot; & CInt(Request(&quot;txtID&quot;)(i))

cn.execute mysql
 
The problem with that code, as I said earlier, is it will only pull the checkboxes that are checked but pull all the text boxes called txtID, so your code won't match the check box to the right ID.

Change your check box to this
<input type=&quot;checkbox&quot; name=&quot;chkStatus<%=rs(&quot;ID&quot;)%>&quot; value=&quot;0&quot;>


And sql loop to this

for i=1 to request(&quot;chkStatus&quot;).count
tempID=int(mid(request(&quot;chkStatus&quot;),10))
SQL=&quot;Update Table Set Status = 1 Where ID = &quot; & tempID
cn.execute mysql
next

This will pull back the checked check boxes, strip of the identifying ID number and update the table setting status to 1 for correct record ID.
I presume it should be set to 1 since you're pulling records that have status of 0 to start with.



 
Since you change the checkbox name to
chkStatus<%=rs(&quot;ID&quot;)%>

The For loop need to change to:

For i=1 to Request(&quot;chkStatus<%=RsData(&quot;TaskID&quot;)%>&quot;).count

but it gave me syntax error
 
D'oh good point
for each x in request.form
if left(request(x),9)=&quot;chkStatus&quot; then
tempID=int(mid(request(x),10))
SQL=&quot;Update Table Set Status = 1 Where ID = &quot; & tempID
cn.execute mysql
End if
next



 
Hmmm that should be...

for each x in request.form
if left(x,9)=&quot;chkStatus&quot; then
tempID=int(mid(x,10))
SQL=&quot;Update Table Set Status = 1 Where ID = &quot; & tempID
cn.execute mysql
End if
next



 
Doing a loop connection isnt a very good idea as far as performace... because you are doing multiple connections if I were doing which I believe I have before... I would do it like this it only requires 2 insert connections:

Name all your checkboxes one name.

1) Put the id of each record in the value of the check box

2) Store all id's in a hidden form field...

1, 2, 3, 4, 5, 6

3) Then do a script to remove the ids that were checked...

So then you have:

strChecked = &quot;1, 2, 3&quot;

strNotChecked = &quot;4, 5, 6&quot;

Then do to INSERTS...

'# Checked values
strInsert = &quot;INSERT INTO table SET status=1 WHERE id in(&quot; & strChecked & &quot;)&quot;


'# UnChecked values
strInsert2 = &quot;INSERT INTO table SET status=0 WHERE id in(&quot; & strNotChecked & &quot;)&quot;


- Jason
Let me know if you need further help
or if this works for you..

www.vzio.com
ASP WEB DEVELOPMENT



 
snowboardr,

I have tried your way yet but How do I get the strChecked or strUnChecked?

Code:
<%Do While NOT RsData.EOF%>
<tr>
<td><input type=&quot;hidden&quot; 
<td>Status<input type=&quot;checkbox&quot; name=&quot;chkStatus&quot; value=&quot;<%Rs(&quot;Status&quot;)Rs(&quot;ID&quot;)%>&quot;>>/td>
<%RsData.MoveNext
Loop%>
 
This should work for you, let me know if you have any problems and I will fix them, I did not test it because I would have had to set up a db and stuff...But you shouldn't have any problems I don't think.

Code:
<%
'#####################################
'## Page 1
'#####################################

Dim sCheckID
Dim sALLidValues


Do While NOT RsData.EOF

sCheckID = Rs(&quot;Status&quot;)Rs(&quot;ID&quot;)

'## When doing looped data
'## Don't switch from html to asp its better on
'## performance if you only do one or the other..
'## Using More response.write's instead of &'s is better
'## On performace as well.

Response.write &quot;<tr>&quot;
Response.write &quot;<td><input type='hidden'&quot;
Response.Write &quot;<td>Status<input type='checkbox' name='chkStatus' value='&quot; 
Response.Write sCheckID 
Response.Write  &quot;'>>/td>&quot;

'# Keep track of all id's
sALLidValues = sALLidValues & sCheckID & &quot; &quot;


RsData.MoveNext
Loop

'# Write a hidden field somewhere after the above code
'# Put all the check ids in it, Trim out end spaces

Response.Write &quot;<input name='hiddenid' type='hidden'value='&quot; & Trim(sALLidValues) & &quot;'>&quot;



'#####################################
'## Page 2
'#####################################


Dim strChecked, sALLidValues   '2 form fields
Dim strAllIdCheckSplit
Dim strCheckedSplit
Dim strUnchecked


sALLidValues = Request.Form(&quot;hiddenid&quot;)
strChecked = Replace(Request.Form(&quot;chkStatus&quot;),&quot;, &quot;, &quot; &quot;)


strAllIdCheckSplit = Split(sALLidValues, &quot; &quot;)

'########################################
'# Here we just go through each
'# id in that was displayed and seprate it
'# depending on it was checked or not
'########################################

For each IdInCheck in strAllIdCheckSplit
   If Instr(strChecked, IdInCheck) > 0 then
	strChecked = strChecked & &quot; &quot; & IdInCheck
		Else
	strUnchecked = strUnchecked & &quot; &quot; & IdInCheck
  End If	
Next

strChecked = Replace(Trim(strChecked), &quot; &quot;, &quot;, &quot;)
strUnchecked = Replace(Trim(strUnchecked), &quot; &quot;, &quot;, &quot;)
			
' strChecked values
' strUnchecked values
' Are now ready for the db...
	
%>


Good luck!

www.vzio.com
ASP WEB DEVELOPMENT



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top