Checkbox Validation on Dynamic ASP Checkboxes

C

clinttoris

Correct Ben, yes I was aware that I did not need the clng function and
it was removed so only If objRS("Question_ID") <> strCurrentQuestionID
Then shows.

The error I am receiving is the same just different line number because
the variable lines shifted the code lines

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/itsurvey/samtestingben.asp, line 56
 
C

clinttoris

Correct Ben, yes I was aware that I did not need the clng function and
it was removed so only If objRS("Question_ID") <> strCurrentQuestionID
Then shows.

The error I am receiving is the same just different line number because
the variable lines shifted the code lines

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/itsurvey/samtestingben.asp, line 56
 
B

Ben Amada

Correct Ben, yes I was aware that I did not need the clng function and
it was removed so only If objRS("Question_ID") <> strCurrentQuestionID
Then shows.

The error I am receiving is the same just different line number because
the variable lines shifted the code lines

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/itsurvey/samtestingben.asp, line 56

Is line 56 the <If objRS("Question_ID") <> strCurrentQuestionID Then> line?

You do have the <strCurrentQuestionID = 0> line in there after the variable
declaration at the top?

Maybe you can post your ASP code again (the entire thing).

Ben
 
C

clinttoris

yes line 56 is the <If objRS("Question_ID") <> strCurrentQuestionID
Then> line


<BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
marginheight="0" bottommargin="0" bgcolor="#ffffff">
<table width="60%" cellspacing="0" cellpadding="5">
<%
Dim iQuestionID
Dim iCheckboxID
Dim strCurrentQuestionID
iQuestionID = 0
iCheckboxID = 0
strCurrentQuestionID = 0

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "DSN=Oracle;uid=rpt_user;pwd=rpt#user;"
'now find the quiz specified by the URL command line
'e.g. vbquiz.asp?quiz=1 means show quiz 1
survey=1
set objRS = oConn.Execute("SELECT * FROM Survey WHERE Survey_ID=" &
survey)
%>
<hr>
<font size=5>
<center><b><% =objRS("Descr") %></center></b>
<HR>
</font>
<p>
<form action="testSubmission.asp" method="post" name="SurveySubmitted">

<%
objRS.close
'now start the main loop to find all the questions

set objRS = oConn.Execute("SELECT * FROM survey_questions WHERE
Survey_ID=" & survey & " ORDER BY Question_ID")

If not objRS.eof Then
%>
<input type="hidden" name="HiddenSurveyID"
value="<%=objRS("Survey_ID")%>">
<input type="hidden" name="HiddenQuestionID"
value="<%=objRS("Question_ID")%>">
<%
End IF

if not objRS.EOF then
objRS.movefirst
do
response.write "<b>" & objRS("Question_ID") & ". " &
objRS("Question") & "</b><p>" & chr(13)
'now display the available options
set objTxt = oConn.Execute("SELECT * FROM survey_user_text WHERE
Question_ID=" & objRS("Question_ID"))
if not objTxt.EOF then
objTxt.movefirst
do

If (objTxt("Type")) = "radio" then

If objRS("Question_ID") <> strCurrentQuestionID Then

If iQuestionID > 0 Then
response.write "<input type=""hidden"" " & "id=""cbCount" &
iQuestionID & """ " & "value=""" & iCheckboxID & """>"
End If

strCurrentQuestionID = objRS("Question_ID")
iQuestionID = iQuestionID + 1
iCheckboxID = 0

'response.write "Question #" & iQuestionID & "<br>"
End If

iCheckboxID = iCheckboxID + 1

response.write "<input type=""checkbox"" id=""" & "chk" & iQuestionID
& "_" & iCheckboxID & """ " & "Name=""Question" & objRS("Question_ID")
& """ Value=""" & objTxt("Sub_Text") & """>" & objTxt("Text") &
"</input><br>" & chr(13)

ELSE
response.write "<BR>" & objTxt("Text")& "<BR>" %><textarea
name="textBoxAnswer<%=objRS("Question_Id")%>" rows="2"
cols="50"></textarea>
<input type="hidden" name="HiddenTextValue"
value="<%=objTxt("sub_text")%>">
<%
End IF
objTxt.movenext
loop until objTxt.EOF
end if
response.write "<p>"
objRS.movenext
loop until objRS.EOF

If iQuestionID > 0 Then
response.write "<input type=""hidden"" " & "id=""cbCount" &
iQuestionID & """ " & "value=""" & iCheckboxID & """>"
End If
response.write "<input type=""hidden"" " & "id=""QuestionCount"" " &
"value=""" & iQuestionID & """>"
End if
oConn.close
%>
<hr>
</TABLE>
<TABLE>
<TR>
<TD COLSPAN=3 ALIGN="center"><BR>
<input type=submit value="Submit" ONCLICK="validateCheckBoxes();">
</TD>
</TR>
</TABLE>
</FORM>
</CENTER>
 
B

Ben Amada

yes line 56 is the <If objRS("Question_ID") <> strCurrentQuestionID
Then> line

Unfortunately at this point, nothing obvious stands out to me. However,
here's something to try:

Replace ...
strCurrentQuestionID = 0
.... with ...
strCurrentQuestionID = ""
.... then change ...
If objRS("Question_ID") <> strCurrentQuestionID Then
.... to ...
If CStr(objRS("Question_ID")) <> CStr(strCurrentQuestionID) Then
.... then change ...
strCurrentQuestionID = objRS("Question_ID")
.... to ...
strCurrentQuestionID = CStr(objRS("Question_ID"))

This will force all of these statements to be evaluated as Strings which
should avoid the type mismatch errors.

Ben
 
C

clinttoris

O.K Ben I have made the changes and I no longer get the error. Any
ideas why the error with the numeric.

Also,
I have added the javascript code and did not fill in any check boxes.
I did receive the error "not all questions answered" both when the user
would not fill in checkboxes and when he/she fills in checkboxes. So
it's not working accordingly. It also proceeds to my thankyou.asp page
with having the user first correct the missing checked checkboxes. Any
ideas?
 
C

clinttoris

I made the changes and it worked however no ideas on why it would not
work with the correct numeric type.

Also, I added the javascript function and it doesn;t appear to work.
It prompts the user with the specified error if he/she both has not
checked anything and has checked something. It also continues to my
thankyou.asp page with having the user first adding checks to the
answers.
 
B

Ben Amada

O.K Ben I have made the changes and I no longer get the error. Any
ideas why the error with the numeric.

Good! Not sure exactly, however it may have to do with the exact numeric
type -- integer, long, decimal, double, etc. Converting everything to a
string avoids this.
Also,
I have added the javascript code and did not fill in any check boxes.
I did receive the error "not all questions answered" both when the user
would not fill in checkboxes and when he/she fills in checkboxes. So
it's not working accordingly. It also proceeds to my thankyou.asp page
with having the user first correct the missing checked checkboxes. Any
ideas?

To avoid going to the thankyou.asp page when not all questions are answered,
you'll want to make two changes:

[1] Change your submit button HTML to:
<input type=submit value="Submit"
ONCLICK="return validateCheckBoxes();">

[2] Change the JavaScript to the following:

function validateCheckBoxes(){

var bOK = true;
var bFlag = false;
var iCheckboxCount = 0;
var iQuestionCount = document.getElementById('QuestionCount').value;

for (var i=1; i <= iQuestionCount; i++) {
bFlag = false;
iCheckboxCount = document.getElementById('cbCount' + i).value;

for (var j=1; j <= iCheckboxCount; j++) {
if (document.getElementById("chk" + i + "_" + j).checked) {
bFlag = true;
}
}

if (!bFlag) { bOK = false; }
}

if (!bOK) {
alert("not all questions answered");
return false;
} else {
return true;
//var oForm = document.SurveySubmitted;//change to real name
//oForm.submit();
}

}
 
C

clinttoris

O.k,

Still having issue now when the user answers a question(checks) at
least one check box for each question I still get the javascript error
"not all questions answered" and this is when it should submit to the
thankyou.asp page.

here is the HTML
<HEAD>
<TITLE>Survey</TITLE>
<script language="JavaScript">
<!--
//Detect IE5.5+
version=0
if (navigator.appVersion.indexOf("MSIE")!=-1){
temp=navigator.appVersion.split("MSIE")
version=parseFloat(temp[1])
}

if (version < 5.02) {
window.location="include/error.html";
}

function validateCheckBoxes(){


var bOK = true;
var bFlag = false;
var iCheckboxCount = 0;
var iQuestionCount = document.getElementById('QuestionCount').value;


for (var i=1; i <= iQuestionCount; i++) {
bFlag = false;
iCheckboxCount = document.getElementById('cbCount' + i).value;


for (var j=1; j <= iCheckboxCount; j++) {
if (document.getElementById("chk" + i + "_" + j).checked) {
bFlag = true;
}
}


if (!bFlag) { bOK = false; }
}


if (!bOK) {
alert("not all questions answered");
return false;
} else {
return true;
//var oForm = document.SurveySubmitted;//change to real name
//oForm.submit();
}
}

//-->
</script>
</head>
<CENTER>

<BODY leftmargin="0" rightmargin="0" marginwidth="0" topmargin="0"
marginheight="0" bottommargin="0" bgcolor="#ffffff">
<table width="60%" cellspacing="0" cellpadding="5">

<hr>
<font size=5>
<center><b>IT Survey Request</center></b>
<HR>
</font>
<p>
<form action="testSubmission.asp" method="post" name="SurveySubmitted">


<input type="hidden" name="HiddenSurveyID" value="1">
<input type="hidden" name="HiddenQuestionID" value="1">
<b>1. Which of the following RIM Communication vehicles do you read
regularly? Please mark all that apply.</b><p>
<input type="checkbox" id="chk1_1" Name="Question1" Value="a">Dispatch
Newsletter</input><br>
<input type="checkbox" id="chk1_2" Name="Question1" Value="b">General
Notifications(email)</input><br>
<input type="checkbox" id="chk1_3" Name="Question1" Value="c">Intranet
Homepage (InSite)</input><br>
<input type="checkbox" id="chk1_4" Name="Question1" Value="d">IT
Service Desk Corporate Notifications</input><br>
<input type="checkbox" id="chk1_5" Name="Question1" Value="e">Team
Websites (http://go/it, http://go/cso etc.)</input><br>
<BR>Other<BR><textarea name="textBoxAnswer1" rows="2"
cols="50"></textarea>
<input type="hidden" name="HiddenTextValue" value="f">
<p><b>2. Please select the 3 most helpful means of
communication?</b><p>
<input type="hidden" id="cbCount1" value="5"><input type="checkbox"
id="chk2_1" Name="Question2" Value="a">Online Newsletter</input><br>
<input type="checkbox" id="chk2_2" Name="Question2" Value="b">Print
Newsletter</input><br>
<input type="checkbox" id="chk2_3" Name="Question2" Value="c">Intranet
homepage (Insite)</input><br>
<p><b>3. Please select the three least helpful means of
communication</b><p>
<p><b>5. To ensure you stay well informed about IT services, projects
and updates, what communication vehicle would you read? Please select
one</b><p>
<p><input type="hidden" id="cbCount2" value="3"><input type="hidden"
id="QuestionCount" value="2">
<hr>
</TABLE>
<TABLE>
<TR>
<TD COLSPAN=3 ALIGN="center"><BR>
<input type=submit value="Submit" ONCLICK="return
validateCheckBoxes();">
</TD>
</TR>
</TABLE>
</FORM>
</CENTER>



Ben said:
O.K Ben I have made the changes and I no longer get the error. Any
ideas why the error with the numeric.

Good! Not sure exactly, however it may have to do with the exact numeric
type -- integer, long, decimal, double, etc. Converting everything to a
string avoids this.
Also,
I have added the javascript code and did not fill in any check boxes.
I did receive the error "not all questions answered" both when the user
would not fill in checkboxes and when he/she fills in checkboxes. So
it's not working accordingly. It also proceeds to my thankyou.asp page
with having the user first correct the missing checked checkboxes. Any
ideas?

To avoid going to the thankyou.asp page when not all questions are answered,
you'll want to make two changes:

[1] Change your submit button HTML to:
<input type=submit value="Submit"
ONCLICK="return validateCheckBoxes();">

[2] Change the JavaScript to the following:

function validateCheckBoxes(){

var bOK = true;
var bFlag = false;
var iCheckboxCount = 0;
var iQuestionCount = document.getElementById('QuestionCount').value;

for (var i=1; i <= iQuestionCount; i++) {
bFlag = false;
iCheckboxCount = document.getElementById('cbCount' + i).value;

for (var j=1; j <= iCheckboxCount; j++) {
if (document.getElementById("chk" + i + "_" + j).checked) {
bFlag = true;
}
}

if (!bFlag) { bOK = false; }
}

if (!bOK) {
alert("not all questions answered");
return false;
} else {
return true;
//var oForm = document.SurveySubmitted;//change to real name
//oForm.submit();
}

}

-------------------------------------
If there's any other problems remaining, please post the rendered HTML
(right-click, View Source).

Ben
 
B

Ben Amada

O.k,

Still having issue now when the user answers a question(checks) at
least one check box for each question I still get the javascript error
"not all questions answered" and this is when it should submit to the
thankyou.asp page.
<TABLE>
<TR>
<TD COLSPAN=3 ALIGN="center"><BR>
<input type=submit value="Submit" ONCLICK="return
validateCheckBoxes();">
</TD>
</TR>
</TABLE>

After a quick test here, it appears that the line break in the submit
input tag is causing problems. Make sure the following line of code is
all on one line without any line breaks:

<input type=submit value="Submit" ONCLICK="return
validateCheckBoxes();">

Otherwise, everything looks good and runs correctly when testing on my
side.

You might also want to add a closing </body> and </html> tag to the end
of your document. Currently, the HTML just ends with a </center> tag.

Let me know if the above correction doesn't fix the problem.

Ben
 
C

clinttoris

Hey Ben,

My submit was on one line and that was not causing the problems. What
I did notice is that if I put a checkmark in every single answer for
each question it will submit. All I want from our original discussion
was that the user needs to just select one answer for each question
before a submission occurs.
 
B

Ben Amada

Hey Ben,

My submit was on one line and that was not causing the problems. What
I did notice is that if I put a checkmark in every single answer for
each question it will submit. All I want from our original discussion
was that the user needs to just select one answer for each question
before a submission occurs.

Actually, the JavaScript should do exactly what you are saying. Over
here, all I need to do is check one box for each question for the form
to submit. I'm wondering if some JavaScript error is occurring on your
end. Which browser/version are you using? I'm running IE6 on Windows
XP SP2.

Below is an updated version of the JavaScript that includes some
message boxes for debugging purposes. Try running the page with this
JavaScript to see if you get any message box alerts.

Ben

-----------------------

function validateCheckBoxes(){

var bOK = true;
var bFlag = false;
var iCheckboxCount = 0;
var iCheckCount = 0;
var iQuestionCount = document.getElementById('QuestionCount').value;

alert("Total number of checkbox questions = " + iQuestionCount);

for (var i=1; i <= iQuestionCount; i++) {
bFlag = false;
iCheckboxCount = document.getElementById('cbCount' + i).value;
iCheckCount = 0;

for (var j=1; j <= iCheckboxCount; j++) {
if (document.getElementById("chk" + i + "_" + j).checked) {
iCheckCount += 1;
bFlag = true;
}
}

alert("Number " + i + ", " + iCheckCount + " out of " +
iCheckboxCount + " are checked.");

if (!bFlag) { bOK = false; }
}

if (!bOK) {
alert("not all questions answered");
return false;
} else {
return true;
//var oForm = document.SurveySubmitted;//change to real name
//oForm.submit();
}

}
 
C

clinttoris

O.k now we are talking. Not sure what the difference was but now it is
working.

Ben can I ask you one final question since you are a genious. Do you
know how to insert date time into an oracle database. What I mean is
how would I get the current date time when the user submits the form to
the database. Here is my sql insert that does work for all the other
parameters but not for date time. I also think there is something
wrong with my double and single quotes but I cannot see it. The
following works to add date but not time. How do I get datetime into
an oracle database. Thanks again for all your help.

CurDate = year(date) & left("00",2-len(month(date))) & month(date) &
left("00",2-len(day(date))) & day(date)

ssqlCheckBox = "INSERT INTO
Survey_User_Answer(Survey_Id,Question_Id,Answer_Id,Date_Answer,User_ID)
VALUES (" & SurveyID & ",'" & objRS("Question_Id") & "' , '" &
answerChoices(answerChoice) & "' , TO_DATE("&CurDate&"), 'user')"
 
B

Ben Amada

O.k now we are talking. Not sure what the difference was but now it is
working.

Yes, strange how the JavaScript started working out of the blue!
Ben can I ask you one final question since you are a genious. Do you
know how to insert date time into an oracle database. What I mean is
how would I get the current date time when the user submits the form to
the database. Here is my sql insert that does work for all the other
parameters but not for date time. I also think there is something
wrong with my double and single quotes but I cannot see it. The
following works to add date but not time. How do I get datetime into
an oracle database. Thanks again for all your help.

CurDate = year(date) & left("00",2-len(month(date))) & month(date) &
left("00",2-len(day(date))) & day(date)

ssqlCheckBox = "INSERT INTO
Survey_User_Answer(Survey_Id,Question_Id,Answer_Id,Date_Answer,User_ID)
VALUES (" & SurveyID & ",'" & objRS("Question_Id") & "' , '" &
answerChoices(answerChoice) & "' , TO_DATE("&CurDate&"), 'user')"

I'm definitely no genius when it comes to Oracle -- I've actually never
used it. However, Oracle should have some type of getdate() function
like SQL Server does. Unless you're using the CurDate variable for
something in your ASP code, you really don't need it at all which
should greatly simplify your INSERT sql statement. If this was an
INSERT statement for SQL Server, it would look like:

ssqlCheckBox = "INSERT INTO Survey_User_Answer (Survey_Id, Question_Id,
Answer_Id, Date_Answer, User_ID) VALUES (" & SurveyID & ",'" &
objRS("Question_Id") & "' , '" & answerChoices(answerChoice) & "' ,
getdate(), 'user')"

Note how CurDate is not needed and getdate() will insert the current
date AND time.

I would try to find out what the built-in Oracle function is for
getting the current date and time either from another newsgroup or it
should be in a manual somewhere.

Good luck,
Ben
 
C

clinttoris

Ben so sorry I just realized one other problem not with the datatime
but with the checkboxes. If the user fills in something for
other(which is a textbox) it should still let the user submit. So what
I am saying for some questions you may have checkbox answers or an
answer called other that is a textbox. If the user fills in the
textbox this is equivalent to checking a checkbox. Sorry.

As for the oracle date function it is sysdate. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top