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.
2 5503
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... -
'First, the push to the database.
-
If Request.Form("CB0") <> Empty Then
-
sTemp = sTemp & Request.Form("CB0") & " "
-
End If
-
If Request.Form("CB1") <> Empty Then
-
sTemp = sTemp & Request.Form("CB1") & " "
-
End If
-
If Request.Form("CB2") <> Empty Then
-
sTemp = sTemp & Request.Form("CB2") & " "
-
End If
-
'...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.
-
sTemp = Replace( Trim( sTemp ), " ", "," )
-
'Now push the info into your DB.
-
oRs("sSelections") = sTemp
-
-
'Second, to pull the data out of the db and manipulate...
-
sSQL = "SELECT sSelections FROM tblTable WHERE User = '" & User & "';"
-
Set oRs = Server.CreateObject("ADODB.RecordSet")
-
oRs.Open sSQL, Connection, adOpenReadOnly, adLockOptimistic, adCmdText
-
If oRs.EOF = False Then
-
sTemp = oRs("sSelections")
-
End If
-
oRs.Close
-
Set oRs = Nothing
-
aTemp = Split(sTemp, ",") 'Will split the selections by the comma
-
If IsArray(aTemp) Then
-
For i = 0 To UBound(aTemp)
-
Response.Write aTemp(i)
-
Next
-
Else
-
Response.Write aTemp
-
End If
-
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
Hello theblissfulwizard,
I did not know what type of database you are using so this example is using Microsoft Access.
PageOne.asp -
<%
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")
-
Set rsList = Server.CreateObject("ADODB.Recordset")
-
SQL = "SELECT Article_ID, Title FROM TheTable"
-
rsList.CursorType = 1
-
rsList.LockType = 3
-
rsList.Open SQL, Conn
-
%>
-
<html>
-
<head>
-
<title>Page One</title>
-
<script language="JavaScript">
-
function Validate()
-
{
-
var TempArticlesArray = "";
-
-
for(var i = 0; i < <%=rsList.RecordCount%>; i++)
-
{
-
if (document.getElementsByName('CB' + i).item(0).checked)
-
{
-
TempArticlesArray = TempArticlesArray + document.getElementsByName('CB' + i).item(0).value;
-
}
-
if (!(i==<%=rsList.RecordCount%>))
-
{
-
TempArticlesArray = TempArticlesArray + ", ";
-
}
-
}
-
-
document.xForm.ArticlesArray.value=TempArticlesArray;
-
}
-
</script>
-
</head>
-
<body>
-
<form method="post" action="PageTwo.asp" name="xForm" id="xForm">
-
<input type="hidden" name="ArticlesArray" value="">
-
<%i = 0%>
-
<%Do Until (rsList.EOF)%>
-
<input type="checkbox" name="CB<%=i%>" value="<%=rsList("Article_ID").value%>"> <%=rsList("Title").value%><br>
-
<%i = (i + 1)%>
-
<%rsList.MoveNext%>
-
<%Loop%>
-
<input type="submit" value="Submit" class="button" onClick="Validate();">
-
</form>
-
</body>
-
</html>
-
PageTwo.asp -
<%
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("YourDatabase.mdb")
-
-
Function GetArticleTitle(Article_ID)
-
Set rsArticleTitle = Server.CreateObject("ADODB.Recordset")
-
SQL = "SELECT Title FROM TheTable WHERE Article_ID = '" & Article_ID & "'"
-
rsArticleTitle.CursorType = 1
-
rsArticleTitle.LockType = 3
-
rsArticleTitle.Open SQL, Conn
-
-
GetArticleTitle = rsArticleTitle("Title").value
-
End Function
-
-
ArticlesArray = Split(Request.Form("ArticlesArray"), ",")
-
%>
-
<html>
-
<head>
-
<title>Page Two</title>
-
</head>
-
<body>
-
<table>
-
<tr>
-
<td>Location In Array</td>
-
<td> </td>
-
<td>Value of Location</td>
-
<td> </td>
-
<td>Title from Database</td>
-
</tr>
-
<%For i = 0 To (UBound(ArticlesArray)-1)%>
-
<tr>
-
<td><%Response.Write(i)%></td>
-
<td> </td>
-
<td><%Response.Write(ArticlesArray(i))%></td>
-
<td> </td>
-
<td><%Response.Write(GetArticleTitle(ArticlesArray(i)))%></td>
-
</tr>
-
<%Next%>
-
</table>
-
</body>
-
</html>
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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()...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| | |