Page 1 of 1

PHP Error

Posted: Mon Jul 13, 2009 8:49 pm
by andrewj
In the below code
1st - on click of each radio button i want to display the correct choice it's not showing in the form i can get it java script can't

2nd thing: i want to show the selected radio button value in alert each time i click it
it's also not showing

Please help me

Code: Select all

<?php
session_start();
 
//database connectivitiy
include_once("database.php");
$db = new database();
$db->connectdb();
 
$today = date("d-m-Y");
echo $today."<br />";
 
//declare form variables
$sel_tid = $_POST['testid'];
$_SESSION['sel_tid'] = $sel_tid;
$std = $_SESSION['usr'];
 
//to load the number of questions
$sql_num = mysql_query("SELECT no_of_que FROM exam WHERE examid='$sel_tid'");
 
while($res_num = mysql_fetch_array($sql_num))
$n = $res_num[no_of_que];
 
//get time duration
$sql_time = mysql_query("SELECT time_duration FROM exam WHERE examid='$sel_tid'");
while($res_time = mysql_fetch_array($sql_time)){
$time_dur = $res_time[time_duration]; //in minutes
$t_dur_sec = $time_dur*60*1000;
}
?>
 
<!--start html -->
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",$t_dur_sec);
}
 
function stopCount()
{
clearTimeout(t);
}
 
function calMarks(test)
{
//
 
//
alert("Hi");
alert("Correct choice: " + document.test.correct.value);
var totalScore = 0; //initialize to 0
var diff = 0; //initialize to 0
 
alert("Your choice: " + document.test.$res_test[questionid].value);
 
if(document.test.$res_test[questionid].checked){
alert("Your choice: " + document.test.$res_test[questionid].value);
}
 
//f.result.value = eval(f.expr.value)
if(document.test.$res_test[questionid].value = document.test.correct.value){
totalScore++;
alert("Your choice: " + document.test.$res_test[questionid].value);
alert("Your score: " + totalScore);
}
else{
diff++;
alert("Your choice: " + document.test.$res_test[questionid].value);
}
/*
for(y=0; y<4; y++){
alert("Your choice: " + $res_test[questionid][y].value);
}
 
$res_test[questionid] = document.test.$res_test[questionid];
for (x=0; x < $res_test[questionid].length; x++) {
if ( $res_test[questionid][x].checked )
alert($res_test[questionid][x].value);
}
//
/*
var allQuestions = new Array(document.test.$res_test[questionid]);
 
var totalScore = 0; //initialize to 0
 
//go through each question set
for (var i in allQuestions) {
var temp = allQuestions[i];
 
//go through each radio button in the current question set
for (var j = 0; j < temp.length; j++) {
 
 
 
//if the correct one is chosen then add 1 to total score
if(temp[j].checked == true && temp[j]==$correct){
totalScore++;
}
 
/*
if (temp[j].value == "correct" && temp[j].checked == true) {
totalScore++;
}
*/
 
/* alert("Congratulations! Your score is " + totalScore +
" out of " + allQuestions.length + "! " + (totalScore/allQuestions.length));*/
// }
// }
} //end function
</script>
</head>
 
<body>
<form id="timing" name="timing" method="post" action="">
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
<!--<form id="test" name="test" method="post" action="std_viewresult.php"> -->
<form id="test" name="test" method="post" action="std_taketest_trial.php">
<label>Time Duration
<input type="text" name="time_dur" value="<?php echo $time_dur ?>" />
</label>
 
<p>Test Id
<label>
<input type="text" name="tid" value="<?php echo $sel_tid ?>">
</label>
</p>
<p>
<input type="submit" name="show_results" value="Show Results">
<?php
/*Show test*/
//query to load questions
$sql_test = mysql_query("
SELECT DISTINCT * FROM question q
WHERE q.examid='$sel_tid'");
 
if($sql_test){
//echo "Query Successful";
//for($i=0; $i<$n; $i++){
echo '<table border="0">'; //display in table
while($res_test=mysql_fetch_array($sql_test)){
//$i=0;
echo "<tr>";
echo "<td><p class='bodyTextBold'><br />".($i+1).".</p></td>";
echo "<td><p class='bodyTextBold'><br /><b>".$res_test[question]."</b></p></td>";
echo "</tr>";
$i++;
 
//to load the number of responses
//$sql_num_ans = mysql_query("SELECT COUNT(answerid) AS ct_ans FROM answer WHERE questionid='$res_test[questionid]'");
 
//while($res_num_ans = mysql_fetch_array($sql_num_ans))
//$r = $res_num_ans[ct_ans];
 
//echo $r;
$sql_ansid = mysql_query("SELECT answerid FROM answer WHERE questionid='$res_test[questionid]'"); //for answerid
$sql_ans = mysql_query("SELECT answer FROM answer WHERE questionid='$res_test[questionid]'"); //for answer
 
$sql_correct = mysql_query("SELECT answer FROM answer WHERE type='correct' AND questionid='$res_test[questionid]'");
$res_correct = mysql_fetch_array($sql_correct);
$correct = $res_correct[answer];
//echo $correct;
echo '<input type="hidden" name="correct" value="'.$correct.'">';
echo '<input type="text" name="show" value="'.$correct.'">';
 
while($res_ansid = mysql_fetch_array($sql_ansid)){
while($res_ans = mysql_fetch_array($sql_ans)){
//for($j=0; $j<$r; $j++){
echo "<tr>";
echo '<td><input type="radio" name="'.$res_test[questionid].'[]" value="'.$res_ans[answer].'" onClick="calMarks(this.form)"></td><td>'.$res_ans[answer].'</td>';
 
/*start calculations*/
 
/////////$ch = $_POST[''.$res_test[questionid].''];
/////////echo "Your Choice: ".$ch;
 
//$choice=array();
/////////foreach($ch as $res_ans[answer])
 
//for($j=0; $j<$n; $j++)
// $choice[$j]=$res_test[questionid];
 
/*end calculations*/
echo "</tr>";
//echo "ans id: ".$res_ansid[answerid]; //testing purposes
//} //end for loop - no of responses
}// end while loop - answer
} //end while loop - answer id
 
} //end while loop - test
echo '</table>';
//} //end for loop - no of questions
} //end sucess query
else
echo "Query failed ".mysql_error();
 
/*Track student results*/
//////calculate marks
 
/*
//////store exam session details
$sql_examsession = mysql_query("INSERT INTO examsession(examid, id, examdate, marks) VALUES('$sel_tid', '$std', '$today', $marks)");
 
if($sql_examsession)
echo "<br />Exam Session details entered successfully";
else
echo "<br />Error: ".mysql_error();
 
/*
$sql_results = mysql_query("SELECT * FROM examsession WHERE examid='$sel_tid' AND id='$std'");
 
if($sql_resutls)
echo "<br />Query successful";
else
echo "<br />Query Error: ".mysql_error();
 
 
//display in table
echo '<br /><table border="1">';
while($res_results=mysql_fetch_array($sql_results)){
echo "<tr>";
echo "<td><p class='bodyTextBold'>Exam Id:</p>".$res_results[examid]."</td>";
echo "<td><p class='bodyTextBold'>Student Id:</p>".$res_results[id]."</td>";
echo "<td><p class='bodyTextBold'>Exam Date:</p>".$res_results[examdate]."</td>";
echo "<td><p class='bodyTextBold'>Marks:</p>".$res_results[marks]."</td>";
echo "</tr>";
}
echo '</table>';
*/
 
if(isset($_POST['show_results'])){
echo 'Hi<br />';
 
$choice = $_POST['$res_test[questionid]'];
echo $choice;
if($choice==$correct){
$marks = $marks + 1;
echo "<br />Current Marks: ".$marks;
}
else{
$diff = $diff + 1;
echo "Difficulty Level: ".$diff;
}
//
/*$checked = isset($_REQUEST['$res_test[questionid]']);
if($checked) {
foreach($checked as $res_ans[answer]) {
$sql_correct = mysql_query("SELECT answer FROM answer WHERE type='correct' AND questionid='$res_test[questionid]'");
$res_correct = mysql_fetch_array($sql_correct);
$correct = $res_correct[answer];
//echo "Correct response: ".$correct; //for testing purposes
if($checked==$sql_correct){
$marks = $marks + 1;
echo "Current Marks: ".$marks;
}//end if
else{
$diff = $diff + 1;
echo "Difficulty Level: ".$diff;
$sql_diff = mysql_query("UPDATE question SET difficultylevel='(difficultylevel+$diff)' WHERE questionid='$res_test[questionid]'");
}//end else
}//end foreach
}//end if*/
}
 
?>
</p>
<p>
<label>
<!-- <input type="submit" name="show_results" value="Show Results">-->
</label>
</p>
</form></p>
<br /><a href='login module/sessioncheck.php'>Go Back</a>
 
</body>
 
</html>
<!--end html --