473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using arrays to Insert, Update, Retrieve from a Table

FYI - New to ASP

I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do.

I have a page that displays a list of news articles - includes Article_ID, Title, Date. I have a checkbox next to each Title that automatically gets assigned a name of "CB0" - "CB1" and so on, the value of each checkbox includes the Article_ID number (unique to each article).

When a user selects multiple checkboxes and clicks Submit on the form I want the Article_ID to be collected and stored into another Table as a string value like "12,14,15,16,23". I also need to be able to retrieve these Article_ID numbers on another page to display back to the user which articles they have selected. So I need to be able to reparse the string without commas.

I have found many posts about arrays but not in ASP code, so any help would be appreciated.
Feb 6 '08 #1
2 5503
markrawlingson
346 Expert 100+
Well, something tells me you're using .NET, not classic ASP - as per your "I'm using ASP 1.1, not 2.0 - But here's some classic ASP for you anyway. I'm sure it can be easily converted to .NET if that's the case.

I would try something like the below...

Expand|Select|Wrap|Line Numbers
  1. 'First, the push to the database.
  2. If Request.Form("CB0") <> Empty Then
  3.    sTemp = sTemp & Request.Form("CB0") & " "
  4. End If
  5. If Request.Form("CB1") <> Empty Then
  6.    sTemp = sTemp & Request.Form("CB1") & " "
  7. End If
  8. If Request.Form("CB2") <> Empty Then
  9.    sTemp = sTemp & Request.Form("CB2") & " "
  10. End If
  11. '...etc, etc. You'll end up with a string of only the selected checkboxes values, split by a space. You'll probably end up with a useless space at the end, so we'll trim that off. We'll also need to put the commas in place.
  12. sTemp = Replace( Trim( sTemp ), " ", "," )
  13. 'Now push the info into your DB.
  14. oRs("sSelections") = sTemp
  15.  
  16. 'Second, to pull the data out of the db and manipulate...
  17. sSQL = "SELECT sSelections FROM tblTable WHERE User = '" & User & "';"
  18. Set oRs = Server.CreateObject("ADODB.RecordSet")
  19. oRs.Open sSQL, Connection, adOpenReadOnly, adLockOptimistic, adCmdText
  20. If oRs.EOF = False Then
  21.    sTemp = oRs("sSelections")
  22. End If
  23. oRs.Close
  24. Set oRs = Nothing
  25. aTemp = Split(sTemp, ",") 'Will split the selections by the comma
  26. If IsArray(aTemp) Then
  27.    For i = 0 To UBound(aTemp)
  28.       Response.Write aTemp(i)
  29.    Next
  30. Else
  31.    Response.Write aTemp
  32. End If
  33.  
Then you can use aTemp(i) and push it into the database to find the other information for the article, such as its title - to display information back to the user that they can actually understand.

OR.. you can just do it the easy way and create a new record in a sub-table for each article the user chooses. Then you can simply pull the information out using GetRows() in one recordset (using a join, much more efficient).

Sincerely,
Mark
Feb 7 '08 #2
CroCrew
564 Expert 512MB
Hello theblissfulwizard,

I did not know what type of database you are using so this example is using Microsoft Access.

PageOne.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Set Conn = Server.CreateObject("ADODB.Connection")
  3.     Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")
  4.     Set rsList = Server.CreateObject("ADODB.Recordset")
  5.     SQL = "SELECT Article_ID, Title FROM TheTable"
  6.     rsList.CursorType = 1
  7.     rsList.LockType = 3
  8.     rsList.Open SQL, Conn
  9. %>
  10. <html>
  11.     <head>
  12.         <title>Page One</title>
  13.         <script language="JavaScript">
  14.             function Validate()
  15.             {
  16.                 var TempArticlesArray = "";
  17.  
  18.                 for(var i = 0; i < <%=rsList.RecordCount%>; i++) 
  19.                 {
  20.                     if (document.getElementsByName('CB' + i).item(0).checked)
  21.                     {
  22.                         TempArticlesArray = TempArticlesArray + document.getElementsByName('CB' + i).item(0).value;
  23.                     }
  24.                     if (!(i==<%=rsList.RecordCount%>))
  25.                     {
  26.                         TempArticlesArray = TempArticlesArray + ", ";
  27.                     }
  28.                 }
  29.  
  30.                 document.xForm.ArticlesArray.value=TempArticlesArray;
  31.             }
  32.         </script>
  33.     </head>
  34.     <body>
  35.         <form method="post" action="PageTwo.asp" name="xForm" id="xForm">
  36.             <input type="hidden" name="ArticlesArray" value="">
  37.             <%i = 0%>
  38.             <%Do Until (rsList.EOF)%>
  39.                 <input type="checkbox" name="CB<%=i%>" value="<%=rsList("Article_ID").value%>"> <%=rsList("Title").value%><br>
  40.                 <%i = (i + 1)%>
  41.                 <%rsList.MoveNext%>
  42.             <%Loop%>
  43.             <input type="submit" value="Submit" class="button" onClick="Validate();">
  44.         </form>
  45.     </body>
  46. </html>
  47.  
PageTwo.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     Set Conn = Server.CreateObject("ADODB.Connection")
  3.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")
  4.  
  5.     Function GetArticleTitle(Article_ID)
  6.         Set rsArticleTitle = Server.CreateObject("ADODB.Recordset")
  7.         SQL = "SELECT Title FROM TheTable WHERE Article_ID = '" & Article_ID & "'"
  8.         rsArticleTitle.CursorType = 1
  9.         rsArticleTitle.LockType = 3
  10.         rsArticleTitle.Open SQL, Conn
  11.  
  12.         GetArticleTitle = rsArticleTitle("Title").value
  13.     End Function
  14.  
  15.     ArticlesArray = Split(Request.Form("ArticlesArray"), ",")
  16. %>
  17. <html>
  18.     <head>
  19.         <title>Page Two</title>
  20.     </head>
  21.     <body>
  22.         <table>
  23.             <tr>
  24.                 <td>Location In Array</td>
  25.                 <td>&nbsp;</td>
  26.                 <td>Value of Location</td>
  27.                 <td>&nbsp;</td>
  28.                 <td>Title from Database</td>
  29.             </tr>
  30.             <%For i = 0 To (UBound(ArticlesArray)-1)%>
  31.                 <tr>
  32.                     <td><%Response.Write(i)%></td>
  33.                     <td>&nbsp;</td>
  34.                     <td><%Response.Write(ArticlesArray(i))%></td>
  35.                     <td>&nbsp;</td>
  36.                     <td><%Response.Write(GetArticleTitle(ArticlesArray(i)))%></td>
  37.                 </tr>
  38.             <%Next%>
  39.         </table>
  40.     </body>
  41. </html>
  42.  
Feb 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
4
by: jay | last post by:
I am using the dataset object to add a row to a sql server database in vb.net code, as follows: dim drow as DataRow dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter) ds.tables(0).NewRow()...
5
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say...
17
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I...
2
by: Godzilla | last post by:
Dear all, I have a challenge in hand and am not too sure how to accomplish this using stored procedure. I have a table containing about 3 fields, but I need to reorder/renumber a field value...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
3
by: planethax | last post by:
I have multiple arrays that I need to insert/update database and I am not sure how to start, I think I need to use the "foreach" statement and also not sure whether or not to serialize and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.