Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp

Status
Not open for further replies.

LadyDi02

Technical User
May 25, 2006
61
CA
Hello experts,

I need a hand. On my asp submit page I have the following code:
Code:
for i = 1 to 5 ' 5 question form' 


Question = Request.Form("Q" & i) 
Answer = Request.Form("A" & i) 
Comment  = Request.Form("C" & i ) 


ValComment = Replace(Comment, "'", "''") 
ValAnswer = Replace(Answer, "'", "''") 


checkbox = Request.Form("check") 
checkbox = Split(checkbox,", ") 
---------------------------------------------------------------------------­-------------- 
Set oConn = Server.CreateObject("ADODB.Connection") 
oConn.Open "removed for security reasons" 
set sSQL = server.CreateObject ("ADODB.Recordset") 
set sSQL1 = server.CreateObject ("ADODB.Recordset") 
---------------------------------------------------------------------------­----------------- 
IF........somthing here in the checkbox = STRUGGLING HERE please help 
sSQL = "INSERT into Table (ID, Question, Answer, Comments, TimeStamp, 
UserID)" 
sSQL = sSQL &  "VALUES (Test_seq.nextval, '" & Question &"', '" & 
checkbox &"', '" & ValComment &"', sysdate, '"& User & "')" 


Else 
sSQL1 = "INSERT into table (ID, Question, Answer, Comments, TimeStamp, 
UserID)" 
sSQL1 = sSQL1 &  "VALUES (Test_seq.nextval, '" & Question &"', '" & 
ValAnswer &"', '" & ValComment &"', sysdate, '"& User & "')" 
response.write ssql1 & "<BR>" 
End IF 
Next 
response.end


Note: just the bulk of the code has been added so that you get an idea
of what it is that I am talking about.


On my html form I have a combination of dropdownlists and combo
boxes. The checkboxes all have the same name. It is basically 1
question checkbox with the ability to choose 5 answers. I have a
total of 5 questions(1 checkbox question and 3 drop downs and 1
textbox. What I am struggling with is how do I get the answers from
the checkboxes into the database as well as the other form elements.
If it is much easier to not parse the comma separated list then so be
it:) Thanks everyone.

 
The easiest way is to loop through the request form items.
Code:
for I = 1 to request.form.count

if request.form.key(I) = so and so then 

  insert into mydb values (request.form.item(I)

end if

next


This code should get you started


just response.write request.form to see the items.
or you can loop through them by modigying the code aove

Code:
for I = 1 to request.form.count

response.write request.form.key(I) & request.form.item(I) & "<BR>"

next
 
Hi Dashley,

Thanks for the response. I should have mentioned that I definitely know how to loop thorugh the checkboxes to get there values but I think the issue is much greater. I guess I am not sure if it has to do with my database structure. I only have one table and in that table I have ID,Question,Answer,Comments,TimeStamp, Userid. Based on my code(if I can ever get it working) how does the system know that the selections made from the checkbox apply to question 1? To explain, if I use your code, I can see what was selected.....and then I can split those values but now how do I insert those values into the table as well as the subsequent values that the user has choosen for the remaining questions from the form?
So the user comes to the website and see
Question 1. How hold are you
[checkbox]5 yrs old
[checkbox]10 yrs
[checkbox]other
Question 2. what car do you like[drop downlist]
Question 3. what music do you like [drop downlist]
Question 4. Comments[textbox]

Based on my code how do I associate all these questions and answers to i.e Mark? Simply, looping won;t help as I am struggling with the insert of the checkbox vs the other form elements. Sorry if I wasn't clear or maybe misunderstanding. Thanks again for your help.
 
I don't see where you are actually executing the INSERT for each of your 5 INSERT queries.

You need something to the tune of

oConn.Execute(sSQL);

I'll write up an example with your code, (forgive my syntax)
Code:
---------------------------------------------------------------------------­-------------- 
Set oConn = Server.CreateObject("ADODB.Connection") 
oConn.Open "removed for security reasons" 
set sSQL = server.CreateObject ("ADODB.Recordset") 
set sSQL1 = server.CreateObject ("ADODB.Recordset") 
---------------------------------------------------------------------------­----------------- 

for i = 1 to 5 ' 5 question form' 

Question = Request.Form("Q" & i) 
Answer = Request.Form("A" & i) 
Comment  = Request.Form("C" & i ) 

ValComment = Replace(Comment, "'", "''") 
ValAnswer = Replace(Answer, "'", "''") 

sSQL = "INSERT into table (ID, Question, Answer, Comments, TimeStamp, UserID)" 
sSQL1 = sSQL1 &  "VALUES (Test_seq.nextval, '" & Question &"', '" & ValAnswer &"', '" & ValComment &"', sysdate, '"& User & "')" 
oConn.Execute(sSQL);
Response.Write sSQL & "<br />" 
Next 
Response.End

This is what I come up with based on the info I see.
Since you have this :

Answer = Request.Form("A" & i)

That makes me assume you have all the answers contained in form elements that all have the same name. (including your checkbox answer)

My suggestion is to give each element a different name on the form and pull each individually. It makes for longer but cleaner code.

Also, it's generally a bad idea to store ' single quotes in databases if you can avoid it.


[monkey][snake] <.
 
Monksnake,

Thanks for the reply. I really was just giving you the gist of the code. I understand that I did not execute the sql statements but I response.write them. I'm a bit confused as to what you are saying. I have my checkboxes that all have the same name(check) and then my drop downnlist questions all have different names. Where in your code did you insert the checkbox values(after looping through the questions? I was trying to show in my orginal post how I do not understand how to incorporate my checkbox values into the existing code with an insert. What you have shown me I can easily do but it does not incorporate the check box values. I have my questions, answers and comments variable but checkboxes are a bit different. How do I loop and insert checkbox values and all other form elements into the same insert into 1 database table? I guess that is the question. Any ideas? Your code is incomplete for what I am looking for. Thanks again
 
LadyDi02,

Hi. Put this code in a page called sample.asp and play with it. It should get you going down the correct path.

Let me know if you have anymore questions.


Code:
<%@ Language=VBScript %>
<%

dim I
dim sql

If Request.QueryString("pass")= "1" then 

	for i = 1 to Request.Form.Count
		
		'isolate the checkboxes.
		If left(Request.Form.key(i),2)="cb" then 
			If Request.Form.Item(I) = "on" then 
				'you can insert the form.key (cb1,cb2,cb3 etc.. into the database
				Response.Write " <BR>CB-" & Request.Form.key(i) & "found" 
			end if
		end if
		
		If left(Request.Form.Key(i),2) ="tb" then
		'tb for textox  sticick it into the comment field
		If Request.Form.Item(i) <> "" then 
		Response.Write " <BR>textbox-" & Request.Form.key(i) & " populated" 
		end if
		end if
		
		If left(Request.Form.Key(i),3) ="sel" then
		'sel for select box stick the answer into the answer col
		if Request.Form.item(i) <> "" then 
		Response.Write " <BR>Select-" & Request.Form.key(i) & " selected" 
		end if
		end if	
		
	next

end if

 'ID,Question,Answer,Comments,TimeStamp, Userid. 

%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<form action="sample.asp?pass=1" method="post">
<table>
<tr><td><input type=checkbox id=checkbox1 name=cb1>Question1
<tr><td><input type=checkbox id=checkbox2 name=cb2>Question2
<tr><td><input type=checkbox id=checkbox3 name=cb3>Question3
<tr><td><input type=checkbox id=checkbox4 name=cb4>Question3
<tr><td><input type=checkbox id=checkbox5 name=cb5>Question5
<tr><Td><INPUT type="text" id=text1 name=tb1>
<tr><td><SELECT id=select1 name=sel1>
<OPTION></OPTION>
<OPTION>answer 1</OPTION>
<OPTION>answer 2</OPTION>
<OPTION>answer 3</OPTION>
</SELECT>
<tr><td>&nbsp;
<tr><td colspan="2"><INPUT type="submit" value="Submit" id=submit1 name=submit1>
<form>
</BODY>
</HTML>
 
LadyDi02

I forgot to mention. Where I have for example in the code

"Response.Write " <BR>CB-" & Request.Form.key(i) & "found"


this is where you insert staement would go.

-dan
 
This is exactly what I needed. I wanted to use my existing code but this will do. Thanks Dashley.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top