PDA

View Full Version : Problems with if Else not working and getting text from textarea to write to document



PaulG2014
01-03-2015, 10:34 AM
Hi All,
I am trying to set up an online exam for music students, I got the if statement working but can't get the else statement to work. Also when all questions are answered I want to be able to get the results from the textbox area to email to student but I have no idea how to do this. If anyone could help it would be much appreciated, here is the code, Thanks in advance:


<!DOCTYPE html>

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>testexam</title>
<link rel="stylesheet" href="tutor.css" media="all" type="text/css">
<!-- Custom Fonts -->
<link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- JS code from stack overflow; modified to suit application by Paul Gillis.-->
<script type="text/javascript"></script>
</head>
<body>
<div class= "container">
<div class="col-sm-4">
<img class="img- img-responsive img-center" src="img/EXAM.PNG" alt="">
</div>
</div>
<h1>Exam Level 1</h1>
<form name ="testexam">
<ol>
<div><img class="img- img-responsive img-center" src="examimg/1.png" alt=""></div>
</br></br>
<li>Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q1" value = "wrong" >This note is B</input></li>
<li><input type = "radio" name ="q1" value = "correct" >This note is A#</input></li>
<li><input type = "radio" name ="q1" value = "wrong" >This note is C</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/2.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q2" value = "correct">This note is A</input></li>
<li><input type = "radio" name ="q2" value = "wrong">This note is B</input></li>
<li><input type = "radio" name ="q2" value = "wrong">This note is C</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/3.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q3" value = "wrong">This note is D</input></li>
<li><input type = "radio" name ="q3" value = "correct">This note is B</input></li>
<li><input type = "radio" name ="q3" value = "wrong">This note is A</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/4.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q4" value = "wrong">This note is A</input></li>
<li><input type = "radio" name ="q4" value = "wrong">This note is B</input></li>
<li><input type = "radio" name ="q4" value = "correct">This note is G</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/5.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q5" value = "wrong">This note is E</input></li>
<li><input type = "radio" name ="q5" value = "correct">This note is D</input></li>
<li><input type = "radio" name ="q5" value = "wrong">This note is A</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/6.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q6" value = "wrong">This note is A#</input></li>
<li><input type = "radio" name ="q6" value = "wrong">This note is C#</input></li>
<li><input type = "radio" name ="q6" value = "correct">This note is F#</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/7.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q7" value = "correct">This note is G#</input></li>
<li><input type = "radio" name ="q7" value = "wrong">This note is F#</input></li>
<li><input type = "radio" name ="q7" value = "wrong">This note is A#</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/8.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q8" value = "wrong">This note is D</input></li>
<li><input type = "radio" name ="q8" value = "correct">This note is C</input></li>
<li><input type = "radio" name ="q8" value = "wrong">This note is A</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/9.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q9" value = "correct">This note is E</input></li>
<li><input type = "radio" name ="q9" value = "wrong">This note is G</input></li>
<li><input type = "radio" name ="q9" value = "wrong">This note is A</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/10.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q10" value = "correct">This note is C#</input></li>
<li><input type = "radio" name ="q10" value = "wrong">This note is G#</input></li>
<li><input type = "radio" name ="q10" value = "wrong">This note is A#</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/11.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q9" value = "wrong">This note is E</input></li>
<li><input type = "radio" name ="q9" value = "correct">This note is F</input></li>
<li><input type = "radio" name ="q9" value = "wrong">This note is G</input></li>
</ul>
<hr>
<div><img class="img- img-responsive img-center" src="examimg/12.png" alt=""></div>
</br></br>
<li> Question, which is the correct answer</li>
<ul>
<li><input type = "radio" name ="q10" value = "correct">This note is D#</input></li>
<li><input type = "radio" name ="q10" value = "wrong">This note is F#</input></li>
<li><input type = "radio" name ="q10" value = "wrong">This note is C#</input></li>
</ul>
<hr>
</ol>
<input type ="button" value = "hello" onclick="sayhello()"></input>
<input type ="button" value = "submit" onclick ="checkAll()"></input>
<input type ="reset" value = "clear"></input>

<textarea rows ="6" cols = "60" name ="answersBox">Your Answer Results are Listed Below</textarea>

</form>
</body>
<footer>
<script type="text/javascript" src="js/testexam.js"></script>


</script>
</footer>
</html>


Here is the JavaScript code:

function sayhello()
{
alert("hello");

}
var text2display = "";
var answers = new Array(10);
answers[0] = "1.This note is A#\n";
answers[1] = "2.This note is A\n";
answers[2] = "3.This note is B\n";
answers[3] = "4.This note is G\n";
answers[4] = "5.This note is D\n";
answers[5] = "6.This note is F#\n";
answers[6] = "7.This note is G#\n";
answers[7] = "8.This note is C\n";
answers[8] = "9.This note is E\n";
answers[9] = "10.This note is C#\n";
answers[10] = "11.This note is F\n";
answers[11] = "12.This note is D#\n";



var questions = new Array(10);
questions[0] = "q1";
questions[1] = "q2";
questions[2] = "q3";
questions[3] = "q4";
questions[4] = "q5";
questions[5] = "q6";
questions[6] = "q7";
questions[7] = "q8";
questions[8] = "q9";
questions[9] = "q10";


function checkQs(s)
{
var qs = document.getElementsByName(s);
var noOfRadios = qs.length;

for(var i = 0; i < noOfRadios; i++)
{
if(qs[i].checked)
{
if (qs[i].value == "correct")
text2display = text2display + questions[questions.indexOf(s)] + "...Well Done You are Correct\n";
else
text2diaplay = text2display + questions[questions.indexOf(s)] + "...Wrong Answer\n";

break;
}
}
}
function checkAll()
{
for(var i = 0; i < questions.length; i++)
{
checkQs(questions[i]);
{
testexam.answersBox.value = text2display;
}
}
}