...

View Full Version : populate checkboxes on edit form



codefantom
08-27-2005, 06:53 PM
Hello All!!! Another noob with a redundant question (maybe!)

I have a form to edit my database. I coded the post form fine. But I have never had to pull from the database and populate checkboxes (The ones check on the post form). I have no idea where to start.

Here is the code from the post form:

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
Set rs = Conn.Execute("SELECT * DISPUTES")
While NOT RS.EOF
%>

<input type="checkbox" name="DAT_DISPUTE" value="true">
<%=response.write(rs("U_DISPUTES"))%>


<%
rs.MoveNext
wend
Set RS = NOTHING
Conn.Close
%>

I need to make the checkboxes post from the database.
This is not sql or asp.net, just simple asp. If that's what you call it.

Any help would be appreciated.

kaihirst
08-28-2005, 12:41 AM
Hi,

you will need to use a multi dimensional array to populate the information, then display it.

on error resume next (youlls see why in a moment)

dim Checkbox
dim x
dim y
dim temp

function CheckPolulate()

if (request.form("submit name") <> "" then

sql = "statement"
objRS.open sql, objconn


if not objRS.EOF or not objRS.BOF then

Checkbox = CheckPopulate()
else

redim temp(1,1)
temp(0,0 = -1
CheckPopulate = temp
end if
end if
end function
%>


Now then... you can either have the checkboxes on the form ready with an individual element in the array ready if you know how many there will be (if your doing it this way, that is why youll need the on error command due to the uninitialised array error) or,

use a dynamic count of the array and write the form elements dynamically to the page this is if you dont know how many your going to have that is...

your choice..

if you want the code, mail me at
kaihirst@hotmail.co.uk

or if you wnt the challenge of learning to do this yourself, dont!!!!!!!!


ta

kai :thumbsup:

codefantom
08-28-2005, 09:44 PM
I have 2 forms.
add.asp
edit.asp
The add.asp has looping checkboxes pulled from my database. There a 17 checkboxes in all.

Here's the code from the add.asp:
[code]
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
Set rs = Conn.Execute("SELECT * from DISPUTES")

While NOT RS.EOF
%>

</span>

<input type="checkbox" name="DAT_DISPUTE" value="<%=RS("U_DISPUTES")%>"><%=RS("U_DISPUTES")%>



<br>
<%
rs.MoveNext
wend
Set RS = NOTHING
Conn.Close
%>
[code]

This add.asp page works fine.

My problem is on the edit.asp.
I don't know how to loop the checkboxes from the database and display the ticked checkboxes selected from the add.asp

neocool00
08-29-2005, 02:32 PM
@codefantom,
I am assuming that there is a field in the disputes table that is your indicator if the checkbox has been ticked or not. In my example, I will call it "ticked" and assume that it is a bit field (meaning it's either 1 or 0 in the db, but in asp it's either true or false).

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
Set rs = Conn.Execute("SELECT * from DISPUTES")
While NOT RS.EOF
%>
</span>
<input type="checkbox" name="DAT_DISPUTE" value="<%=RS("U_DISPUTES")%>"<%If RS("ticked") Then Response.Write " CHECKED" End If%>><%=RS("U_DISPUTES")%>
<br>
<%
rs.MoveNext
wend
Set RS = NOTHING
Conn.Close
%>

codefantom
08-29-2005, 11:36 PM
This is the code I need modified. I know it works because I took it from a generated page from ASPMAKER. But I'm trying to implement it with the code from my page.

[code]
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
sSqlWrk = "SELECT [U_DISPUTES] FROM [DISPUTES] ORDER BY [U_DISPUTES] Asc"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, conn, 1, 2
If Not rswrk.Eof Then
datawrk = rswrk.GetRows
rowswrk = UBound(datawrk, 2)

'Dim ar_x_DISPUTES, ari_x_DISPUTES
ar_x_DISPUTES = split(DAT_DISPUTE&"",",")
For rowcntwrk = 0 To rowswrk
x_DISPUTESChk = x_DISPUTESChk & "<input type='checkbox' name='DAT_DISPUTE' value='" & datawrk(0, rowcntwrk) & "'"
For ari_x_DISPUTES = 0 to UBound(ar_x_DISPUTES)
If CStr(datawrk(0, rowcntwrk)&"") = Trim(ar_x_DISPUTES(ari_x_DISPUTES)&"") Then
x_DISPUTESChk = x_DISPUTESChk & " checked"
Exit For
End If
Next
x_DISPUTESChk = x_DISPUTESChk & ">" & datawrk(0, rowcntwrk) & EditOptionSeparator(rowcntwrk)
Next
End If
rswrk.Close
Set rswrk = Nothing
Response.Write x_DISPUTESChk
%>
[code]

But know I get this error message:

[code]
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'EditOptionSeparator'
/myweb/ducredit/includes/inc_edit.asp, line 434
[code]

This won't display unless I take out the " & EditOptionSeparator(rowcntwrk)" portion

When I do take it out it goes back to my original problem
displayed checkboxes on my edit page without repopulating the previous choses.

Does anyone know what this "EditOptionSeparator" is and can it be change to something else so that it works.

Can anyone help!??

codefantom
08-30-2005, 12:37 AM
I found the function that this is attached to "EditOptionSeparator" but there has to be a simplier way to write this code.

I will show both parts this time

The form element:
[code]
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
sSqlWrk = "SELECT [U_DISPUTES] FROM [DISPUTES] ORDER BY [U_DISPUTES] Asc"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, conn, 1, 2
If Not rswrk.Eof Then
datawrk = rswrk.GetRows
rowswrk = UBound(datawrk, 2)

'Dim ar_x_DISPUTES, ari_x_DISPUTES
ar_x_DISPUTES = split(DAT_DISPUTE&"",",")
For rowcntwrk = 0 To rowswrk
x_DISPUTESChk = x_DISPUTESChk & "<input type='checkbox' name='DAT_DISPUTE' value='" & datawrk(0, rowcntwrk) & "'"
For ari_x_DISPUTES = 0 to UBound(ar_x_DISPUTES)
If CStr(datawrk(0, rowcntwrk)&"") = Trim(ar_x_DISPUTES(ari_x_DISPUTES)&"") Then
x_DISPUTESChk = x_DISPUTESChk & " checked"
Exit For
End If
Next
x_DISPUTESChk = x_DISPUTESChk & ">" & datawrk(0, rowcntwrk) & EditOptionSeparator(rowcntwrk)
x_DISPUTESChk = x_DISPUTESChk & "<BR>"
Next
End If
rswrk.Close
Set rswrk = Nothing
Response.Write x_DISPUTESChk
%>
[code]

And the function:

[code]
' Function to generate Edit Option Separator based on current row count (Radio / CheckBox)
' rowcnt - zero based row count
'
Function EditOptionSeparator(rowcnt)

EditOptionSeparator = "&nbsp;"
' Sample code to adjust 2 options per row
'If ((rowcnt + 1) Mod 2 = 0) Then ' 2 options per row
'EditOptionSeparator = EditOptionSeparator & "<br>"
'End If

End Function
%>
[code]

Any help is appreciated.

Bullschmidt
08-31-2005, 12:17 AM
Here's how I like to deal with checkboxes.

Classic ASP Design Tips - Checkboxes
http://www.bullschmidt.com/devtip-checkboxes.asp



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum