Click to See Complete Forum and Search --> : hidden field help


Juankayce56
02-23-2004, 10:30 PM
Sorry this is my first time using the forum!!!
My problem is i keep getting an error saying that document.quiz.studentAnswer1.value is num even though it's there and it's a hidden field!! i think the problem is in my function scoreQuiz()
here is my code i can't get it to work can anybody help!!
Thanks



<HTML>
<HEAD>
<TITLE>Math Quiz</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function recordAnswer(question, answer) {
if (question == 1)
document.quiz.studentAnswer1.value = answer;
else if (question == 2)
document.quiz.studentAnswer2.value = answer;
else if (question == 3)
document.quiz.studentAnswer3.value = answer;
}
function scoreQuiz() {
if (document.quiz.studentAnswer1.value == "" || document.quiz.studentAnswer2.value == "" || document.quiz.studentAnswer3.value == "") {
alert("You must answer all of the questions before scoring the quiz.");
/* code here to "cancel" the onSubmit event handler*/
return false;
}
var correct = 0;
if (document.quiz.studentAnswer1.value == document.quiz.answer1.value)
++correct;
if (document.quiz.studentAnswer2.value == document.quiz.answer2.value)
++correct;
if (document.quiz.studentAnswer3.value == document.quiz.answer3.value)
++correct;
alert("You scored " + correct + " answers correctly.");
/* code here to have the onSubmit event handler fire off*/
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Math Quiz</H2><P>
<P>Select the correct answer to the following math problems, then select the Score Quiz button.</P>
<FORM NAME="quiz" onSubmit="return scoreQuiz();">
<P><B>1.</B>&nbsp; 84 divided by 7 is equal to _____.</P>
<P><INPUT TYPE=radio NAME=question1 VALUE="6" onClick="recordAnswer(1, this.value);">6<BR>
<INPUT TYPE=radio NAME=question1 VALUE="7" onClick="recordAnswer(1, this.value);">7<BR>
<INPUT TYPE=radio NAME=question1 VALUE="9" onClick="recordAnswer(1, this.value);">9<BR>
<INPUT TYPE=radio NAME=question1 VALUE="12" onClick="recordAnswer(1, this.value);">12</P>
<P><B>2.</B>&nbsp; What is the value of x in the equation <B>x * 12 = 156</B>.</P>
<P><INPUT TYPE=radio NAME=question2 VALUE="5" onClick="recordAnswer(2, this.value);">5<BR>
<INPUT TYPE=radio NAME=question2 VALUE="11" onClick="recordAnswer(2, this.value);">11<BR>
<INPUT TYPE=radio NAME=question2 VALUE="13" onClick="recordAnswer(2, this.value);">13<BR>
<INPUT TYPE=radio NAME=question2 VALUE="19" onClick="recordAnswer(2, this.value);">19</P>
<P><B>3.</B>&nbsp; What is the square root of 196?</P>
<P><INPUT TYPE=radio NAME=question3 VALUE="7" onClick="recordAnswer(3, this.value);">7<BR>
<INPUT TYPE=radio NAME=question3 VALUE="14" onClick="recordAnswer(3, this.value);">14<BR>
<INPUT TYPE=radio NAME=question3 VALUE="28" onClick="recordAnswer(3, this.value);">28<BR>
<INPUT TYPE=radio NAME=question3 VALUE="98" onClick="recordAnswer(3, this.value);">98</P>
<P><INPUT TYPE="submit" VALUE=" Score Quiz "></P>
<Input Type ="hidden" name "studentAnswer1" value ="12">
<Input Type ="hidden" name "studentAnswer2" value ="13">
<Input Type ="hidden" name "studentAnswer3" value ="14">





</FORM>
</BODY>
</HTML>

fredmv
02-23-2004, 10:31 PM
Welcome to the forums.

You can't just provide us with all of your code and expect us to know what's wrong. What exactly isn't working? What should it be doing? What have you tried? You need to fill us in on these things.

peiguo
02-24-2004, 12:10 AM
The first problem I spotted is that: your hidden fields have wrong syntax. They should be:
<Input Type ="hidden" name="studentAnswer1" value ="12">
<Input Type ="hidden" name="studentAnswer2" value ="13">
<Input Type ="hidden" name="studentAnswer3" value ="14">
But you missed those '=' after name.

peiguo
02-24-2004, 12:15 AM
In your scoreQuiz() function, you referred to document.quiz.answer1.value, which is obviously not defined anywhere, so you have no correct answer to check against.

You can define hidden fields for them as well.

By the way, the use of "name" is old fashioned, for new code, shall use id.

peiguo
02-24-2004, 12:32 AM
This must be what you want:

<HTML>
<HEAD>
<TITLE>Math Quiz</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function recordAnswer(question, answer) {
if (question == 1)
document.quiz.studentAnswer1.value = answer;
else if (question == 2)
document.quiz.studentAnswer2.value = answer;
else if (question == 3)
document.quiz.studentAnswer3.value = answer;
}
function scoreQuiz() {
if (document.quiz.studentAnswer1.value == "" || document.quiz.studentAnswer2.value == "" || document.quiz.studentAnswer3.value == "") {
alert("You must answer all of the questions before scoring the quiz.");
/* code here to "cancel" the onSubmit event handler*/
return false;
}
var correct = 0;
if (document.quiz.studentAnswer1.value == document.quiz.answer1.value)
++correct;
if (document.quiz.studentAnswer2.value == document.quiz.answer2.value)
++correct;
if (document.quiz.studentAnswer3.value == document.quiz.answer3.value)
++correct;
alert("You scored " + correct + " answers correctly.");
/* code here to have the onSubmit event handler fire off*/
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Math Quiz</H2><P>
<P>Select the correct answer to the following math problems, then select the Score Quiz button.</P>
<FORM NAME="quiz" onSubmit="return scoreQuiz();">
<P><B>1.</B> 84 divided by 7 is equal to _____.</P>
<P><INPUT TYPE=radio NAME=question1 VALUE="6" onClick="recordAnswer(1, this.value);">6<BR>
<INPUT TYPE=radio NAME=question1 VALUE="7" onClick="recordAnswer(1, this.value);">7<BR>
<INPUT TYPE=radio NAME=question1 VALUE="9" onClick="recordAnswer(1, this.value);">9<BR>
<INPUT TYPE=radio NAME=question1 VALUE="12" onClick="recordAnswer(1, this.value);">12</P>
<P><B>2.</B> What is the value of x in the equation <B>x * 12 = 156</B>.</P>
<P><INPUT TYPE=radio NAME=question2 VALUE="5" onClick="recordAnswer(2, this.value);">5<BR>
<INPUT TYPE=radio NAME=question2 VALUE="11" onClick="recordAnswer(2, this.value);">11<BR>
<INPUT TYPE=radio NAME=question2 VALUE="13" onClick="recordAnswer(2, this.value);">13<BR>
<INPUT TYPE=radio NAME=question2 VALUE="19" onClick="recordAnswer(2, this.value);">19</P>
<P><B>3.</B> What is the square root of 196?</P>
<P><INPUT TYPE=radio NAME=question3 VALUE="7" onClick="recordAnswer(3, this.value);">7<BR>
<INPUT TYPE=radio NAME=question3 VALUE="14" onClick="recordAnswer(3, this.value);">14<BR>
<INPUT TYPE=radio NAME=question3 VALUE="28" onClick="recordAnswer(3, this.value);">28<BR>
<INPUT TYPE=radio NAME=question3 VALUE="98" onClick="recordAnswer(3, this.value);">98</P>
<P><INPUT TYPE="submit" VALUE=" Score Quiz "></P>
<Input Type ="hidden" name="studentAnswer1" value ="">
<Input Type ="hidden" name="studentAnswer2" value ="">
<Input Type ="hidden" name="studentAnswer3" value ="">
<Input Type ="hidden" name="answer1" value ="12">
<Input Type ="hidden" name="answer2" value ="13">
<Input Type ="hidden" name="answer3" value ="14">
</FORM>
</BODY>
</HTML>


By the way, I personally prefer java variable against hidden field, as that gives you more clear logic. Unless you want submit the answer, there is really no point for hidden field.

peiguo
02-24-2004, 12:46 AM
You can use JavaScript variables instead of hidden fields. You probably want to submit student answers, then use hidden fields for student answers, but for correct answers, really no need.

<SCRIPT LANGUAGE="JavaScript">
studentAnswers = new Array(0, 0, 0);
answers = new Array(12,13,14);

function recordAnswer(question, answer) {
studentAnswers[question] = answer;
}

function scoreQuiz() {
var correct = 0;
for (i = 0; i < answers.length; i ++) {
alert(studentAnswers[i] + "," + answers[i])
if (studentAnswers[i] == answers[i]) {
++correct;
}
}
alert("You got " + correct + " questions answered correctly");
}
</SCRIPT>