This is my rat.php file. This file will display question form. Basically I have 8 questions and these are some of it. I use onsubmit="getScore(this.form) as refer to my Javascript code. My Javascript code is in the display file which is ratResult.php.
However, my display file only capture the answers chosen but not the percentage. There is no error shown but the percentage is the one that I need the most. I've googled several times and still couldn't find the solution. Really hope fellow programmers here can help me.
<div id="form" style="text-align:left">
<br><h2>Please answer all questions</h2><br>
<?PHP print question(1); ?>
<br><input type = "radio" name = "rat1" value = "<?PHP print ansHigh(1); ?>"><?PHP print ansHigh(1); ?><br>
<input type = "radio" name = "rat1" value = "<?PHP print ansLow(1); ?>"><?PHP print ansLow(1); ?><br><br>
<?PHP print question(2); ?>
<br><input type = "radio" name = "rat2" value = "<?PHP print ansHigh(2); ?>"><?PHP print ansHigh(2); ?><br>
<input type = "radio" name = "rat2" value = "<?PHP print ansLow(2); ?>"><?PHP print ansLow(2); ?><br><br>
This is the section in file ratResult.php to display all answers chosen and the percentage.
<div id="test" style="text-align:left">
<?php
if(empty($_POST)===false){
$rat1 = $_POST['rat1'];
$rat2 = $_POST['rat2'];
$rat3 = $_POST['rat3'];
$rat4 = $_POST['rat4'];
$rat5 = $_POST['rat5'];
$rat6 = $_POST['rat6'];
$rat7 = $_POST['rat7'];
$rat8 = $_POST['rat8'];
echo "<h4>RAT</h4>";
echo "Answer 1: ".$rat1."<br>";
echo "Answer 2: ".$rat2."<br>";
echo "Answer 3: ".$rat3."<br>";
echo "Answer 4: ".$rat4."<br>";
echo "Answer 5: ".$rat5."<br>";
echo "Answer 6: ".$rat6."<br>";
echo "Answer 7: ".$rat7."<br>";
echo "Answer 8: ".$rat8."<br>";
$insert = mysql_query('INSERT INTO ratresult(username,q1,q2,q3,q4,q5,q6,q7,q8,dateTaken)VALUES("'.$loginSession.'","'.$rat1.'"
,"'.$rat2.'","'.$rat3.'","'.$rat4.'","'.$rat5.'","'.$rat6.'","'.$rat7.'","'.$rat8.'",now())') or die (mysql_error($insert));
}
else
echo "error!";
?>
Score = <input type=text size=15 name="percentage"><br>
<hr /><a href=main.php>Back To Main Page </a>
This is the Java Script to calculate the percentage.
<script>
var numQues = 8;
var numChoi = 2;
var answers = new Array(8);
answers[0] = "Yes";
answers[1] = "Above 50 years old";
answers[2] = "Below 12 years old";
answers[3] = "Yes";
answers[4] = "No";
answers[5] = "Yes";
answers[6] = "Yes";
answers[7] = "Above 50 years old";
function getScore(form) {
var score = 0;
var currElt;
var currSelection;
for (i=0; i<numQues; i++) {
currElt = i*numChoi;
for (j=0; j<numChoi; j++) {
currSelection = form.elements[currElt + j];
if (currSelection.checked) {
if (currSelection.value == answers[i]) {
score++;
break;
}
}
}
}
score = Math.round(score/numQues*100);
form.percentage.value = score + "%";
var correctAnswers = "";
for (i=1; i<=numQues; i++) {
correctAnswers += i + ". " + answers[i-1] + "\r\n";
}
form.solutions.value = correctAnswers;
}
</script>
Aucun commentaire:
Enregistrer un commentaire