Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: ticking a checkbox with JavaScript is a condition pulled from a database is met


Message #1 by "Dean" <spinout@i...> on Wed, 30 Jan 2002 14:20:07
Hello,



I am trying to write a Javascript that will check a checkbox if certain 

conditions are met. These conditions will be pulled from an SQL server 

database using ASP/SQL. 



Background:

I am writing a login page. Each user will have access to certain areas. 

For example:



- Change Web Site Poll

- Change Web Site Forum

- Change Web Site News



etc.

If I want to modify a users account and give them more access to other 

areas they previously do not have access to I want to be able to bring up 

their details on a page and have Javascript already tick the checkboxes 

for the areas stored in the database which they are already granted access 

to.



For example: If I create a login 'billgates' and he has access to change 

the web site poll but not the web site forum or web site news, I want the 

page to load and tick the checkbox for the web site poll, but not the web 

site forum or web site news.



I hope that isn't a little too confusing. I have tried to explain it as 

simply and clearly as possible.



Any help would be greatly appreciated. Thanks!
Message #2 by "Craig Flannigan" <ckf@k...> on Wed, 30 Jan 2002 14:32:02 -0000
I have a similar system in use, but I do not use Javascript for it.



in the line that contains the Checkbox, I simply place a condition statement

that checks the value of the field





For Example:



<input type="checkbox" name="Approve" value="1" <% if objRST(13) then

response.write "Checked" %>>





This line above checks field 13, and if it contains a value (I.E it's

ticked) then the ASP alters the HTML to provide the textbox ticked.



I don't see why you'd need to do it in Javascript.







Hope this helps...

Craig.







-----Original Message-----

From: Dean [mailto:spinout@i...]

Sent: 30 January 2002 14:20

To: ASP Databases

Subject: [asp_databases] ticking a checkbox with JavaScript is a

condition pulled from a database is met





Hello,



I am trying to write a Javascript that will check a checkbox if certain

conditions are met. These conditions will be pulled from an SQL server

database using ASP/SQL.



Background:

I am writing a login page. Each user will have access to certain areas.

For example:



- Change Web Site Poll

- Change Web Site Forum

- Change Web Site News



etc.

If I want to modify a users account and give them more access to other

areas they previously do not have access to I want to be able to bring up

their details on a page and have Javascript already tick the checkboxes

for the areas stored in the database which they are already granted access

to.



For example: If I create a login 'billgates' and he has access to change

the web site poll but not the web site forum or web site news, I want the

page to load and tick the checkbox for the web site poll, but not the web

site forum or web site news.



I hope that isn't a little too confusing. I have tried to explain it as

simply and clearly as possible.



Any help would be greatly appreciated. Thanks!




$subst('Email.Unsub').



_____________________________________________________________________

This message has been checked for all known viruses by Star Internet

delivered through the MessageLabs Virus Scanning Service on behalf of

Kingfield Heath Ltd. For further information visit

http://www.star.net.uk/stats.asp





_____________________________________________________________________

This message has been checked for all known viruses by Star Internet

delivered through the MessageLabs Virus Scanning Service on behalf of Kingfield Heath Ltd. For further information visit
http://www.star.net.uk/stats.asp

Message #3 by "Dean" <spinout@i...> on Wed, 30 Jan 2002 14:52:13
You may have a very good point there, I may not need to use Javascript. 

I'll give your example a try and see how I go. Thanks for that.





> I have a similar system in use, but I do not use Javascript for it.

> 

> in the line that contains the Checkbox, I simply place a condition 

statement

> that checks the value of the field

> 

> 

> For Example:

> 

> <input type="checkbox" name="Approve" value="1" <% if objRST(13) then

> response.write "Checked" %>>

> 

> 

> This line above checks field 13, and if it contains a value (I.E it's

> ticked) then the ASP alters the HTML to provide the textbox ticked.

> 

> I don't see why you'd need to do it in Javascript.

> 

> 

> 

> Hope this helps...

> Craig.

> 

> 

> 

> -----Original Message-----

> From: Dean [mailto:spinout@i...]

> Sent: 30 January 2002 14:20

> To: ASP Databases

> Subject: [asp_databases] ticking a checkbox with JavaScript is a

> condition pulled from a database is met

> 

> 

> Hello,

> 

> I am trying to write a Javascript that will check a checkbox if certain

> conditions are met. These conditions will be pulled from an SQL server

> database using ASP/SQL.

> 

> Background:

> I am writing a login page. Each user will have access to certain areas.

> For example:

> 

> - Change Web Site Poll

> - Change Web Site Forum

> - Change Web Site News

> 

> etc.

> If I want to modify a users account and give them more access to other

> areas they previously do not have access to I want to be able to bring up

> their details on a page and have Javascript already tick the checkboxes

> for the areas stored in the database which they are already granted 

access

> to.

> 

> For example: If I create a login 'billgates' and he has access to change

> the web site poll but not the web site forum or web site news, I want the

> page to load and tick the checkbox for the web site poll, but not the web

> site forum or web site news.

> 

> I hope that isn't a little too confusing. I have tried to explain it as

> simply and clearly as possible.

> 

> Any help would be greatly appreciated. Thanks!




> $subst('Email.Unsub').

> 

> _____________________________________________________________________

> This message has been checked for all known viruses by Star Internet

> delivered through the MessageLabs Virus Scanning Service on behalf of

> Kingfield Heath Ltd. For further information visit

> http://www.star.net.uk/stats.asp

> 

> 

> _____________________________________________________________________

> This message has been checked for all known viruses by Star Internet

> delivered through the MessageLabs Virus Scanning Service on behalf of 

Kingfield Heath Ltd. For further information visit 

http://www.star.net.uk/stats.asp

Message #4 by "Drew, Ron" <RDrew@B...> on Wed, 30 Jan 2002 11:29:21 -0500
Since Javascript is client you really want to use the server to fill in

the checkbox....you need to use <% %> code embedded in the input

statement to test for the database field to have a value and then do a

response.write as "checked"



-----Original Message-----

From: Dean [mailto:spinout@i...]

Sent: Wednesday, January 30, 2002 9:20 AM

To: ASP Databases

Subject: [asp_databases] ticking a checkbox with JavaScript is a

condition pulled from a database is met





Hello,



I am trying to write a Javascript that will check a checkbox if certain

conditions are met. These conditions will be pulled from an SQL server

database using ASP/SQL.



Background:

I am writing a login page. Each user will have access to certain areas.

For example:



- Change Web Site Poll

- Change Web Site Forum

- Change Web Site News



etc.

If I want to modify a users account and give them more access to other

areas they previously do not have access to I want to be able to bring

up

their details on a page and have Javascript already tick the checkboxes

for the areas stored in the database which they are already granted

access

to.



For example: If I create a login 'billgates' and he has access to change



the web site poll but not the web site forum or web site news, I want

the

page to load and tick the checkbox for the web site poll, but not the

web

site forum or web site news.



I hope that isn't a little too confusing. I have tried to explain it as

simply and clearly as possible.



Any help would be greatly appreciated. Thanks!




$subst('Email.Unsub').

Message #5 by "Dean" <spinout@i...> on Wed, 30 Jan 2002 16:39:17
I tried the example you suggested. It worked for the first checkbox but 

not the others. The setup I have in my database is as follows



Table Users

Which contains FullName, UserName etc



Table Areas

Which contains a link back to the Users Table on the UserName field then 

another field called AccessAreas.

Any user in the Users Table can have many different AccessAreas in the 

Areas table.



So when I do a loop for ASP to go through 5 checkbox's and check against 

the database whether a user has access to that specific area (and 

therefore ticks the checkbox) it either makes a complete mess of it or 

does nothing at all.



What I am using is the following





<% While Not RsAreas.EOF %>



<INPUT type="checkbox" name="Area1" <% If RsAreas("AccessAreas") = "Area1" 

then Response.Write "checked" %>>



<INPUT type="checkbox" name="Area2" <% If RsAreas("AccessAreas") = "Area2" 

then Response.Write "checked" %>>



<INPUT type="checkbox" name="Area3" <% If RsAreas("AccessAreas") = "Area3" 

then Response.Write "checked" %>>



<% RsAreas.MoveNext

Wend %>



That makes a huge mess.



The way to solve this would be to do it this way: (in my opinion anyhow)



<INPUT type="checkbox" name="Area1" <% While Not RsAreas.EOF If RsAreas

("AccessAreas") = "Area1" then Response.Write "checked" RsAreas.MoveNext 

Wend %>>



<INPUT type="checkbox" name="Area2" <% While Not RsAreas.EOF If RsAreas

("AccessAreas") = "Area2" then Response.Write "checked" RsAreas.MoveNext 

Wend %>>





<INPUT type="checkbox" name="Area3" <% While Not RsAreas.EOF If RsAreas

("AccessAreas") = "Area3" then Response.Write "checked" RsAreas.MoveNext 

Wend %>>



but then that seems to do nothing at all. No ASP errors or anything, but 

also no data seems to be extracted from the database. Is there some 

specific reason why doing it like that would cause this problem? Can you 

have more than one While Not loop on a page for the same Rs object?



Dean.









> > I have a similar system in use, but I do not use Javascript for it.

> > 

> > in the line that contains the Checkbox, I simply place a condition 

> statement

> > that checks the value of the field

> > 

> > 

> > For Example:

> > 

> > <input type="checkbox" name="Approve" value="1" <% if objRST(13) then

> > response.write "Checked" %>>

> > 

> > 

> > This line above checks field 13, and if it contains a value (I.E it's

> > ticked) then the ASP alters the HTML to provide the textbox ticked.

> > 

> > I don't see why you'd need to do it in Javascript.

> > 

> > 

> > 

> > Hope this helps...

> > Craig.

> > 

> > 

> > 

> > -----Original Message-----

> > From: Dean [mailto:spinout@i...]

> > Sent: 30 January 2002 14:20

> > To: ASP Databases

> > Subject: [asp_databases] ticking a checkbox with JavaScript is a

> > condition pulled from a database is met

> > 

> > 

> > Hello,

> > 

> > I am trying to write a Javascript that will check a checkbox if certain

> > conditions are met. These conditions will be pulled from an SQL server

> > database using ASP/SQL.

> > 

> > Background:

> > I am writing a login page. Each user will have access to certain areas.

> > For example:

> > 

> > - Change Web Site Poll

> > - Change Web Site Forum

> > - Change Web Site News

> > 

> > etc.

> > If I want to modify a users account and give them more access to other

> > areas they previously do not have access to I want to be able to bring 

up

> > their details on a page and have Javascript already tick the checkboxes

> > for the areas stored in the database which they are already granted 

> access

> > to.

> > 

> > For example: If I create a login 'billgates' and he has access to 

change

> > the web site poll but not the web site forum or web site news, I want 

the

> > page to load and tick the checkbox for the web site poll, but not the 

web

> > site forum or web site news.

> > 

> > I hope that isn't a little too confusing. I have tried to explain it as

> > simply and clearly as possible.

> > 

> > Any help would be greatly appreciated. Thanks!




> > $subst('Email.Unsub').

> > 

> > _____________________________________________________________________

> > This message has been checked for all known viruses by Star Internet

> > delivered through the MessageLabs Virus Scanning Service on behalf of

> > Kingfield Heath Ltd. For further information visit

> > http://www.star.net.uk/stats.asp

> > 

> > 

> > _____________________________________________________________________

> > This message has been checked for all known viruses by Star Internet

> > delivered through the MessageLabs Virus Scanning Service on behalf of 

> Kingfield Heath Ltd. For further information visit 

> http://www.star.net.uk/stats.asp

Message #6 by "Peter Foti (PeterF)" <PeterF@S...> on Wed, 30 Jan 2002 12:12:30 -0500
You need to move back to the first record before you can loop through

the records a second time.  I think this is what you mean to do:



<%

Dim SQLStr

Dim RsAreas



' First, create a recordset containing the AccessAreas of a user

SQLStr = "SELECT AccessAreas FROM Areas WHERE UserName='" & username &

"'"



' Create recordset object

Set RsAreas = Server.CreateObject("ADODB.Recordset")

Set RsAreas = DConn.Execute( SQLStr, ,adCmdText )



' The dirty approach

' For each input object, loop through the entire 

' recordset to see if there is a match

Response.Write("<INPUT type=""checkbox"" name=""Area1"" ")

While Not RsAreas.EOF 

   If RsAreas("AccessAreas") = "Area1" Then

      Response.Write("checked")

   End If

   RsAreas.MoveNext 

Wend

Response.Write(">")



Response.Write("<INPUT type=""checkbox"" name=""Area2"" ")

RsAreas.MoveFirst

While Not RsAreas.EOF 

   If RsAreas("AccessAreas") = "Area2" Then

      Response.Write("checked") 

   End If

   RsAreas.MoveNext 

Wend

Response.Write(">")



Response.Write("<INPUT type=""checkbox"" name=""Area3"" ")

RsAreas.MoveFirst

While Not RsAreas.EOF 

   If RsAreas("AccessAreas") = "Area3" Then

      Response.Write("checked")

   End If

   RsAreas.MoveNext 

Wend

Response.Write(">")

	

' Close and terminate recordset object

RsAreas.close

Set RsAreas = Nothing

%>



This approach is not exactly very clean.  In fact, I think that if there

are no records, you will get an error when you try to MoveFirst.  To

avoid this, you should first count the number of records.  If the count

= 0, then there are no records, so don't even try to do .MoveFirst.



I'll leave the rest for you.  Hope this helps. :)

Peter





> -----Original Message-----

> From: Dean [mailto:spinout@i...]

> Sent: Wednesday, January 30, 2002 4:39 PM

> To: ASP Databases

> Subject: [asp_databases] RE: ticking a checkbox with JavaScript is a

> condition pulled from a database is met

> 

> 

> I tried the example you suggested. It worked for the first 

> checkbox but 

> not the others. The setup I have in my database is as follows

> 

> Table Users

> Which contains FullName, UserName etc

> 

> Table Areas

> Which contains a link back to the Users Table on the UserName 

> field then 

> another field called AccessAreas.

> Any user in the Users Table can have many different 

> AccessAreas in the 

> Areas table.

> 

> So when I do a loop for ASP to go through 5 checkbox's and 

> check against 

> the database whether a user has access to that specific area (and 

> therefore ticks the checkbox) it either makes a complete mess 

> of it or 

> does nothing at all.

> 

> What I am using is the following

> 

> 

> <% While Not RsAreas.EOF %>

> 

> <INPUT type="checkbox" name="Area1" <% If 

> RsAreas("AccessAreas") = "Area1" 

> then Response.Write "checked" %>>

> 

> <INPUT type="checkbox" name="Area2" <% If 

> RsAreas("AccessAreas") = "Area2" 

> then Response.Write "checked" %>>

> 

> <INPUT type="checkbox" name="Area3" <% If 

> RsAreas("AccessAreas") = "Area3" 

> then Response.Write "checked" %>>

> 

> <% RsAreas.MoveNext

> Wend %>

> 

> That makes a huge mess.

> 

> The way to solve this would be to do it this way: (in my 

> opinion anyhow)

> 

> <INPUT type="checkbox" name="Area1" <% While Not RsAreas.EOF 

> If RsAreas

> ("AccessAreas") = "Area1" then Response.Write "checked" 

> RsAreas.MoveNext 

> Wend %>>

> 

> <INPUT type="checkbox" name="Area2" <% While Not RsAreas.EOF 

> If RsAreas

> ("AccessAreas") = "Area2" then Response.Write "checked" 

> RsAreas.MoveNext 

> Wend %>>

> 

> 

> <INPUT type="checkbox" name="Area3" <% While Not RsAreas.EOF 

> If RsAreas

> ("AccessAreas") = "Area3" then Response.Write "checked" 

> RsAreas.MoveNext 

> Wend %>>

> 

> but then that seems to do nothing at all. No ASP errors or 

> anything, but 

> also no data seems to be extracted from the database. Is there some 

> specific reason why doing it like that would cause this 

> problem? Can you 

> have more than one While Not loop on a page for the same Rs object?

> 

> Dean.

> 

> 

> 

> 

> > > I have a similar system in use, but I do not use 

> Javascript for it.

> > > 

> > > in the line that contains the Checkbox, I simply place a 

> condition 

> > statement

> > > that checks the value of the field

> > > 

> > > 

> > > For Example:

> > > 

> > > <input type="checkbox" name="Approve" value="1" <% if 

> objRST(13) then

> > > response.write "Checked" %>>

> > > 

> > > 

> > > This line above checks field 13, and if it contains a 

> value (I.E it's

> > > ticked) then the ASP alters the HTML to provide the 

> textbox ticked.

> > > 

> > > I don't see why you'd need to do it in Javascript.

> > > 

> > > 

> > > 

> > > Hope this helps...

> > > Craig.

> > > 

> > > 

> > > 

> > > -----Original Message-----

> > > From: Dean [mailto:spinout@i...]

> > > Sent: 30 January 2002 14:20

> > > To: ASP Databases

> > > Subject: [asp_databases] ticking a checkbox with JavaScript is a

> > > condition pulled from a database is met

> > > 

> > > 

> > > Hello,

> > > 

> > > I am trying to write a Javascript that will check a 

> checkbox if certain

> > > conditions are met. These conditions will be pulled from 

> an SQL server

> > > database using ASP/SQL.

> > > 

> > > Background:

> > > I am writing a login page. Each user will have access to 

> certain areas.

> > > For example:

> > > 

> > > - Change Web Site Poll

> > > - Change Web Site Forum

> > > - Change Web Site News

> > > 

> > > etc.

> > > If I want to modify a users account and give them more 

> access to other

> > > areas they previously do not have access to I want to be 

> able to bring 

> up

> > > their details on a page and have Javascript already tick 

> the checkboxes

> > > for the areas stored in the database which they are 

> already granted 

> > access

> > > to.

> > > 

> > > For example: If I create a login 'billgates' and he has access to 

> change

> > > the web site poll but not the web site forum or web site 

> news, I want 

> the

> > > page to load and tick the checkbox for the web site poll, 

> but not the 

> web

> > > site forum or web site news.

> > > 

> > > I hope that isn't a little too confusing. I have tried to 

> explain it as

> > > simply and clearly as possible.

> > > 

> > > Any help would be greatly appreciated. Thanks!




> > > $subst('Email.Unsub').

> > > 

> > > 

> _____________________________________________________________________

> > > This message has been checked for all known viruses by 

> Star Internet

> > > delivered through the MessageLabs Virus Scanning Service 

> on behalf of

> > > Kingfield Heath Ltd. For further information visit

> > > http://www.star.net.uk/stats.asp

> > > 

> > > 

> > > 

> _____________________________________________________________________

> > > This message has been checked for all known viruses by 

> Star Internet

> > > delivered through the MessageLabs Virus Scanning Service 

> on behalf of 

> > Kingfield Heath Ltd. For further information visit 

> > http://www.star.net.uk/stats.asp

> 




> $subst('Email.Unsub').

> 

Message #7 by "Dean" <spinout@i...> on Wed, 30 Jan 2002 17:48:15
Thanks Peter

Basically my code looks pretty much the same as in your example. Except I 

didn't have the RsAreas.MoveFirst before the While Not RsAreas.EOF 

statement. However, adding MoveFirst doesn't seem to have changed 

anything. I still get all of the checkboxes unticked. When I remove the 

While Not statement then the checkbox containing the first match is 

ticked, but not any other match's after the first.... obviously I need the 

While Not statement in there to loop through and check for any other 

match's for that username, however that's where the problems occur. It's 

for sure a problem with having all of those While Not loop's on there that 

it doesn't like... because of those it for some reason does not extract 

any data from the database although I know there are at least 2 match's in 

the database. Adding MoveFirst hasn't seemed to have changed anything. It 

still spits out the page with all the checkboxes unticked although I know 

at least 2 of them are a match and therefore should be ticked. I'm totally 

puzzled.



> You need to move back to the first record before you can loop through

> the records a second time.  I think this is what you mean to do:

> 

> <%

> Dim SQLStr

> Dim RsAreas

> 

> ' First, create a recordset containing the AccessAreas of a user

> SQLStr = "SELECT AccessAreas FROM Areas WHERE UserName='" & username &

> "'"

> 

> ' Create recordset object

> Set RsAreas = Server.CreateObject("ADODB.Recordset")

> Set RsAreas = DConn.Execute( SQLStr, ,adCmdText )

> 

> ' The dirty approach

> ' For each input object, loop through the entire 

> ' recordset to see if there is a match

> Response.Write("<INPUT type=""checkbox"" name=""Area1"" ")

> While Not RsAreas.EOF 

>    If RsAreas("AccessAreas") = "Area1" Then

>       Response.Write("checked")

>    End If

>    RsAreas.MoveNext 

> Wend

> Response.Write(">")

> 

> Response.Write("<INPUT type=""checkbox"" name=""Area2"" ")

> RsAreas.MoveFirst

> While Not RsAreas.EOF 

>    If RsAreas("AccessAreas") = "Area2" Then

>       Response.Write("checked") 

>    End If

>    RsAreas.MoveNext 

> Wend

> Response.Write(">")

> 

> Response.Write("<INPUT type=""checkbox"" name=""Area3"" ")

> RsAreas.MoveFirst

> While Not RsAreas.EOF 

>    If RsAreas("AccessAreas") = "Area3" Then

>       Response.Write("checked")

>    End If

>    RsAreas.MoveNext 

> Wend

> Response.Write(">")

> 	

> ' Close and terminate recordset object

> RsAreas.close

> Set RsAreas = Nothing

> %>

> 

> This approach is not exactly very clean.  In fact, I think that if there

> are no records, you will get an error when you try to MoveFirst.  To

> avoid this, you should first count the number of records.  If the count

> = 0, then there are no records, so don't even try to do .MoveFirst.

> 

> I'll leave the rest for you.  Hope this helps. :)

> Peter
Message #8 by "Dean" <spinout@i...> on Wed, 30 Jan 2002 18:28:06
I've managed to get it working. 

The reason I thought adding RsArea.MoveFirst wasn't doing anything 

different to the problem I had before I added it was obviously a cache 

problem because I cleared my cache, re-loaded the new version of the ASP 

document with the RsArea.MoveFirst added... and whaddya know! It worked!

So technically the problem was I didn't have the MoveFirst command before 

each additional While loop.... then when Peter pointed this out and I 

added it, that was no longer the problem, it was as simple as emptying out 

my browser cache.



Thanks again to everyone who offered me assistance!
Message #9 by "Peter Foti (PeterF)" <PeterF@S...> on Wed, 30 Jan 2002 14:21:15 -0500
Hi Dean,



Glad you got it working.



A better approach would be to create an array to hold the names of the

input tags you want to display on the page, and to get all of the

records into an array using .getRows, and then comparing the values in

one array against the other instead of using a recordset (it's faster,

and more efficient).





<%

' Names of the input tags - keep in sync with the database

Const NumOfAreas = 5

Dim AreaNames(5)

AreaNames(1) = "Area1"

AreaNames(2) = "Area2"

AreaNames(3) = "Area3"

AreaNames(4) = "Area4"

AreaNames(5) = "Area5"





' Reusable function for opening a connection

Function OpenDBConn( ConnString, DConn )

	Set DConn = Server.CreateObject("ADODB.Connection")	

	DConn.Open ConnString

End Function





' Reusable function for closing a connection

Function CloseDBConn( DConn )

	DConn.Close

	Set DConn = Nothing

End Function



' Function for getting the records from the database

Function GetAccessAreas( DConn, username, AccessArray )

	Dim SQLStr

	Dim oRS

	

	SQLStr = "SELECT AccessAreas FROM Areas WHERE UserName='" &

username & "' ORDER BY AccessAreas"

	

	' Create recordset object

	Set oRS = Server.CreateObject("ADODB.Recordset")

	Set oRS = DConn.Execute( SQLStr, ,adCmdText )

	

	' Copy recordset into an array

	If Not oRS.EOF Then

		AccessArray = oRS.getrows

	Else

		' If no records found, assign it zero so when we check

to see if it is an array, it will fail

		AccessArray = 0

	End If

	

	' Close and terminate recordset object

	oRS.close

	set oRS = Nothing

End Function





' Open Connection

' ConnStr is defined elsewhere as the connection string

Call OpenDBConn( ConnStr, DBConn )



' Get Access Areas

' UName is defined elsewhere as the username to look up

Call GetAccessAreas( DBConn, UName, AccessAreasArray )



' Close Connection

Call CloseDBConn( DBConn )



' Display Input boxes

If Not IsArray(AccessAreasArray ) Then

	' No results found in the database, so write out the inputs with

none checked

	For i = 1 to NumOfAreas

		Response.Write("<input type=""checkbox"" name=""" &

AreaNames(i) & """>")

	Next

	

Else

	' Results found, so write out the inputs with some checked

	NumRecords = ubound(AccessAreasArray ,2) + 1



	For i = 1 to NumOfAreas

		Response.Write("<input type=""checkbox"" name=""" &

AreaNames(i) & """")

		' Recurse the array to see if we have a match

		For j = 1 to NumRecords

			' If we have a match, write checked

			If AccessAreasArray(0, j - 1) = AreaNames(i)

Then

				Response.write(" checked")

			End If

		Next

		' Close the input tag

		Response.Write(">")

	Next

End If

%>



Note that I have not tested this code, but I believe it will work.  :)

-Pete





> -----Original Message-----

> From: Dean [mailto:spinout@i...]

> Sent: Wednesday, January 30, 2002 5:48 PM

> To: ASP Databases

> Subject: [asp_databases] RE: ticking a checkbox with 

> JavaScript is a c-

> ondition pulled from a database is met

> 

> 

> Thanks Peter

> Basically my code looks pretty much the same as in your 

> example. Except I 

> didn't have the RsAreas.MoveFirst before the While Not RsAreas.EOF 

> statement. However, adding MoveFirst doesn't seem to have changed 

> anything. I still get all of the checkboxes unticked. When I 

> remove the 

> While Not statement then the checkbox containing the first match is 

> ticked, but not any other match's after the first.... 

> obviously I need the 

> While Not statement in there to loop through and check for any other 

> match's for that username, however that's where the problems 

> occur. It's 

> for sure a problem with having all of those While Not loop's 

> on there that 

> it doesn't like... because of those it for some reason does 

> not extract 

> any data from the database although I know there are at least 

> 2 match's in 

> the database. Adding MoveFirst hasn't seemed to have changed 

> anything. It 

> still spits out the page with all the checkboxes unticked 

> although I know 

> at least 2 of them are a match and therefore should be 

> ticked. I'm totally 

> puzzled.

> 

> > You need to move back to the first record before you can 

> loop through

> > the records a second time.  I think this is what you mean to do:

> > 

> > <%

> > Dim SQLStr

> > Dim RsAreas

> > 

> > ' First, create a recordset containing the AccessAreas of a user

> > SQLStr = "SELECT AccessAreas FROM Areas WHERE UserName='" & 

> username &

> > "'"

> > 

> > ' Create recordset object

> > Set RsAreas = Server.CreateObject("ADODB.Recordset")

> > Set RsAreas = DConn.Execute( SQLStr, ,adCmdText )

> > 

> > ' The dirty approach

> > ' For each input object, loop through the entire 

> > ' recordset to see if there is a match

> > Response.Write("<INPUT type=""checkbox"" name=""Area1"" ")

> > While Not RsAreas.EOF 

> >    If RsAreas("AccessAreas") = "Area1" Then

> >       Response.Write("checked")

> >    End If

> >    RsAreas.MoveNext 

> > Wend

> > Response.Write(">")

> > 

> > Response.Write("<INPUT type=""checkbox"" name=""Area2"" ")

> > RsAreas.MoveFirst

> > While Not RsAreas.EOF 

> >    If RsAreas("AccessAreas") = "Area2" Then

> >       Response.Write("checked") 

> >    End If

> >    RsAreas.MoveNext 

> > Wend

> > Response.Write(">")

> > 

> > Response.Write("<INPUT type=""checkbox"" name=""Area3"" ")

> > RsAreas.MoveFirst

> > While Not RsAreas.EOF 

> >    If RsAreas("AccessAreas") = "Area3" Then

> >       Response.Write("checked")

> >    End If

> >    RsAreas.MoveNext 

> > Wend

> > Response.Write(">")

> > 	

> > ' Close and terminate recordset object

> > RsAreas.close

> > Set RsAreas = Nothing

> > %>

> > 

> > This approach is not exactly very clean.  In fact, I think 

> that if there

> > are no records, you will get an error when you try to MoveFirst.  To

> > avoid this, you should first count the number of records.  

> If the count

> > = 0, then there are no records, so don't even try to do .MoveFirst.

> > 

> > I'll leave the rest for you.  Hope this helps. :)

> > Peter




> $subst('Email.Unsub').

> 


  Return to Index