Forms and Javascript help


OSNN Addict
17 Sep 2002
please see attached source code. this is not the real code of what I am doing, but it is the jist of what I am doing.

I have made a test (Q & A) webpage. I have an NAME input on the form. When it is submitted (graded via javascript) a new window will popup and give results. I want to add to the score results page the name the was inputed on the test, but cant seem to figure out on how to do it.

Alittle help would be appreciated.




  • test.txt
    4.3 KB · Views: 139
I rewrote your script for you :) there were too many errors and bugs to fix, it is now valid HTML too ;)

<html lang="en">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <meta http-equiv="Content-Script-Type" content="text/javascript">
  <title>Your Quiz</title>
<script type='text/javascript'>
//=================== NUMBER OF QUESTIONS AND HOW TO SCORE==================
var radioNum      = 5;              //Total number of questions
var qNum		= 4;			//Number of options per question
var maxPt         = 100;            //Total possible points
var YourScore	= 0;			//Starting score
var correct		= new Array();	//Answer key array
correct[0] = "D";
correct[1] = "B";
correct[2] = "D";
correct[3] = "A";
correct[4] = "C";
var answer		= new Array();	//User answer array

//============== GRADING THE TEST=============

var radioTax      = maxPt / radioNum;  //Points per question
var timeStart 	= new Date();

function CheckSum ()
  for (i=0; i<qNum; i++) {
    if (document.answer.Q1[i].checked == true)
      answer[0] = document.answer.Q1[i].value; 
    if (document.answer.Q2[i].checked == true)
      answer[1] = document.answer.Q2[i].value;
    if (document.answer.Q3[i].checked == true)
      answer[2] = document.answer.Q3[i].value; 
    if (document.answer.Q4[i].checked == true)
      answer[3] = document.answer.Q4[i].value; 
    if (document.answer.Q5[i].checked == true)
      answer[4] = document.answer.Q5[i].value; 
  for (i=0; i<radioNum; i++) {
    if (answer[i] == correct[i])
      YourScore += radioTax;
//==============BUILDING THE SCORE CARD=============

  var scoreSheet ="","","width=350,height=400,toolbars=no,status=no,scrollbars=yes,resizable=yes");
  var timeTurnIn = new Date();
  var firstname = document.answer.firstname.value;
  scoreSheet.document.writeln("<h1 align='center'>Test Results<\/h1>");
  scoreSheet.document.writeln("<b>Name:<\/b> " + firstname + "<br>");
  scoreSheet.document.writeln("<b>Time Started:<\/b> " + timeStart.getHours() + ":" + timeStart.getMinutes() + ":" + timeStart.getSeconds() + "<br>");
  scoreSheet.document.writeln("<b>Time Turned In:<\/b> " + timeTurnIn.getHours() + ":" + timeTurnIn.getMinutes() + ":" + timeTurnIn.getSeconds() + "<br>");
  scoreSheet.document.writeln("<b>Summary:<\/b><br><table width='100%' align='center'>");
  scoreSheet.document.writeln("<tr align='center'><td>Question<\/td><td>Answer<\/td><td>Your Answer<\/td><td>Status<\/td><\/tr>");

  for (var i=0; i<radioNum; i++) {
    scoreSheet.document.write("<tr align='center'><td>" + (i+1) + "<\/td><td>" + correct[i] + "<\/td><td>" + answer[i]);
    if (answer[i] == correct[i])

  scoreSheet.document.writeln("<b>Score:<\/b> " + YourScore + " out of " + maxPt + " possible points.<br>");
  scoreSheet.document.writeln("<b>Comments:<\/b> ");

  if      (YourScore == 100) { 
    scoreSheet.document.writeln("Way to go!!<br>"); 
  else if (YourScore >= 80)  { scoreSheet.document.writeln("Pretty good!<br>"); }
  else if (YourScore >= 60)  { scoreSheet.document.writeln("Come on, you can do better than that!<br>"); }
  else                       { scoreSheet.document.writeln("You need to study harder.<br>"); }


<!-- ***************** QUESTIONS FORM  *******************-->

<form name="answer" onSubmit="return CheckSum(this)" method="post" action="">
Name: <input type="text" name="firstname"><br>

1. If I have 3 apples, and I ate 2 of them, how many apples do I have left?
<input name="Q1" type="radio" value="A">A Four apples
<input name="Q1" type="radio" value="B">B Three apples
<input name="Q1" type="radio" value="C">C Two apples
<input name="Q1" type="radio" value="D">D One apple

2. Ice will turn into water if the temperature is higher than __<sup>o</sup>C?
<input name="Q2" type="radio" value="A">A 50
<input name="Q2" type="radio" value="B">B 0
<input name="Q2" type="radio" value="C">C 100
<input name="Q2" type="radio" value="D">D 32

3. Which of the following is correct?
<input name="Q3" type="radio" value="A">A The Sun is surrounding the Earth
<input name="Q3" type="radio" value="B">B The Earth is the center of the Solar System
<input name="Q3" type="radio" value="C">C The Earth is surrounding the Moon
<input name="Q3" type="radio" value="D">D The Earth is surrounding the Sun

4. Which of the following is NOT a part of speech?<br>
<input name="Q4" type="radio" value="A">A Bold
<input name="Q4" type="radio" value="B">B Verb
<input name="Q4" type="radio" value="C">C Preposition
<input name="Q4" type="radio" value="D">D Noun

5. Who is the first president of United States?
<input name="Q5" type="radio" value="A">A Bill Clinton
<input name="Q5" type="radio" value="B">B Abraham Lincoln
<input name="Q5" type="radio" value="C">C George Washington
<input name="Q5" type="radio" value="D">D Thomas Jefferson

<input type='submit' value='GRADE IT'>
<input type='reset' value='Erase All Answers'>


j79zlr, you da man! You didnt have to go through all of that trouble. But in the end, I got the results I wanted. MUCH MUCH appreciated! some of the errors you did, i didnt worry about simply because it was a working script. I am doing a project for work so I am having like 150+ questions....eeeeeck. i have one test made currently with 100 questions, that works good.

Anyways, enough with the babbling... Thanks again!


Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Latest member