|
I need to look at each element in an array to write it to the web page, one element at a time. The previous page contained a form of dynamically created checkbox sets. The number of checkbox sets will always be unknown. Anyway, here's my question:
I've tried different code (due to circumstances that would be to lengthy to get into here, I need to use vbscript, be able to use javascript within vbscript code.
Here are examples I've tried and the results:
First attempt:
CkBoxArray="("
for each objItem in request.Form()
if left(objItem,5)="cksub" and request.Form(objItem) <> "" then
CkBoxArray=CkBoxArray & request.Form(objItem) & ","
end if
next
theLen=len(CkBoxArray)
CkBoxArray=left(CkBoxArray,theLen-1)
CkBoxArray=CkBoxArray & ")"
response.Write("Checkboxes with value of cksub - CkBoxArray contents are: " & CkBoxArray & "<br>")
'Results for above code are:
Checkboxes with value of cksub - CkBoxArray contents are: (4402, 4403,4201, 4202, 4203)
written to page
Second attempt:
ReDim CkBoxArray(0)
for each objItem in request.Form()
if left(objItem,5)="cksub" and request.Form(objItem) <> "" then
'Store the value entered by the user in the array
CkBoxArray(UBound(CkBoxArray)) = request.Form(objItem)
'grow the array to be one element greater than its current size
'Preserve its contents
ReDim Preserve CkBoxArray(UBound(CkBoxArray) + 1)
end if
next
The for each loop below results in a display of (based on selection):
4402, 4403
4201, 4202, 4203
Both scenarios assume two checkbox sets, but like I said the sets are built dynamically and are unknown.
When I write out to the web page, I want the type of results shown below:
Value of element in array = 4402
Value of element in array = 4403
Value of element in array = 4201
Value of element in array = 4202
Value of element in array = 4203
Eventually I'll be writing each element to its own record in a db, but am currently debugging code just trying to separate each unknown element on its by writing it to the page. I'm pulling out all of my hair on this one....Any suggestions before its all gone??? Thanks!!!!!
<Added>
Sorry, forgot the code for after:
The for each loop below results in a display of (based on selection):
4402, 4403
4201, 4202, 4203
For Each x in CkBoxArray
response.write(x)
Next
|
|
|
|
|
I've revamped my original code and everything works except for grabbing the registerID.
This is the area I'm having problems with:
'execute SQL statement, to get newly created RegisterId
iRegisterId = 1 '***USE THIS AND THE REST OF THE CODE WORKS, BUT DOESN'T GET FROM FIRST TABLE, JUST HARDCODED
'iRegisterID = connupdate("Register_ID") '*** NEED TO USE THIS AND IT DOESN'T WORK??!
Here is the code that worked, except for being able to grab the Register_ID after the record is inserted into the first table, so that I can use in as part of the insert into the second table:
Here is the entire code for the page:
<% Option Explicit %>
<!-- #include file="adovbs.inc" -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<%
Dim aCatIds, _
aSubCatIds
Dim iGroupId, _
iCntCatId, _
iCntSubCatId, _
iSubCatId, _
iRegisterId
Dim FormNotify
Dim strNotify
Dim sFld, _
sSql
Dim connupdate
sSql = "INSERT INTO Webregister (Company_Name, St_Address, City, State, Zip, Phone_Num, Fax_Num, Web_Site, Contact_Person, Contact_Phone, Contact_Email, Respond_Da, E_Notify) Values ('" & Replace(Request.Form("D_Company"), "'", "") & "','" & Replace(Request.Form("D_Address"), "'", "") & "','" & Replace(Request.Form("D_City"), "'", "") & "','" & Replace(Request.Form("D_State"), "'", "") & "','" & Replace(Request.Form("D_Zip"), "'", "") & "','" & Replace(Request.Form("D_Phone"), "'", "") & "','" & Replace(Request.Form("D_Fax"), "'", "") & "','" & Replace(Request.Form("D_Website"), "'", "") & "','" & Replace(Request.Form("D_Contact"), "'", "") & "','" & Replace(Request.Form("D_CPhone"), "'", "") & "','" & Replace(Request.Form("D_Email"), "'", "") & "','" & Replace(Request.Form("hidDate"), "'", "") & "','" & Request.Form("notify") & "')"
set connupdate = server.createobject("ADODB.Connection")
connupdate.open "Registration"
connupdate.execute(sSql)
'execute SQL statement, to get newly created RegisterId
iRegisterId = 1 '***USE THIS AND THE REST OF THE CODE WORKS, BUT DOESN'T GET FROM FIRST TABLE, JUST HARDCODED
'iRegisterID = connupdate("Register_ID") '*** NEED TO USE THIS AND IT DOESN'T WORK??!
aCatIds = Split(Request.Form("GroupID"), ", ")
For iCntCatId = 0 To UBound(aCatIds)
iGroupId = aCatIds(iCntCatId)
aSubCatIds = Split(Request.Form("SubCatId_" & iGroupId), ", ")
For iCntSubCatId = 0 To UBound(aSubCatIds)
iSubCatId = aSubCatIds(iCntSubCatId)
sSql = "INSERT INTO WebCommodities (Register_ID, GroupID, SubCategory) Values(" & vbCrLf & _
" " & iRegisterId & "," & vbCrLf & _
" " & iGroupId & "," & vbCrLf & _
" " & iSubCatId & vbCrLf & _
")"
'execute SQL statement
connupdate.execute(sSql)
Next
Next
connupdate.close
set connupdate = nothing
%>
</BODY>
</HTML>
I know I need to also use adOpenKeySet to make sure I grab the right id.
In my original code, I used this for connection:
set connupdate = server.createobject("ADODB.Connection")
strProvider="Provider=MSDASQL.1;Persist Security Info=False;Data Source=Registration"
connupdate.Open "WebRegister", strProvider, adOpenKeySet, adLockPessimistic
but for some reason it didn't like my new code - any ideas?
|
|
|
|
|
|
|
|