Javascript 使用选定选项显示图像
我正在做一个网站的问题和答案,其中几个选择题将显示,用户将不得不选择单选按钮来回答问题。提交后,会显示正确答案和错误答案,用户可以选择查看正确答案。 在本页中,为了查看正确答案,我想做的是为正确选项显示勾号图像,为所有其他错误选项显示十字图像。但我在写逻辑时遇到了问题。 这就是我到现在为止所做的 此页面用于选择要显示的图像Javascript 使用选定选项显示图像,javascript,php,html,Javascript,Php,Html,我正在做一个网站的问题和答案,其中几个选择题将显示,用户将不得不选择单选按钮来回答问题。提交后,会显示正确答案和错误答案,用户可以选择查看正确答案。 在本页中,为了查看正确答案,我想做的是为正确选项显示勾号图像,为所有其他错误选项显示十字图像。但我在写逻辑时遇到了问题。 这就是我到现在为止所做的 此页面用于选择要显示的图像 <? session_start() ; $wrong = array() ; if( !empty($_SESS
<?
session_start() ;
$wrong = array() ;
if( !empty($_SESSION['wrongAnswer'] ) ) {
$wrong = $_SESSION['wrongAnswer'] ;//wrongAnswer array has
//ids of wrongly answered
//questions
session_destroy() ;
}
if( !empty($_POST['wrongAnswer'] ) ) {
$wrong = $_POST['wrongAnswer'] ;
}
$i = 0 ;
$quesID = "";
$question = "" ;
require_once "connection.php";
foreach ($wrong as $n) {
<? $query = "SELECT * FROM answers WHERE question_id = '$n' " ;
if($result = $mysqli->query( $query ) ) {
if( $row = $result->fetch_object() ){
$answer = $row->answer ;
// $count++;
}
else
echo "Couldn't tally answer " . $mysqli->error ;
}
else
echo "Error executing query" . $mysqli->error ;
//<----- THIS IS WHERE THE WRONGLY ANSWERED QUESTIONS WILL BE DISPLAYED WITH IMAGES AS MENTIONED BEFORE ------>//
$query = "SELECT * FROM questions WHERE questionid='$n' " ;
$i ++ ;
if( $result = $mysqli->query( $query ) ) {
$count= $result->num_rows ;
echo '<form name = "checkans.php" action = "checkans.php" method = "POST" >' ;
$row = $result->fetch_object() ;
// $i++ ;
$quesID = $row->questionid ;
$question = $row->question ;
$quesID = $row->questionid ;
$query2 = "SELECT option1,option2,option3,option4 FROM options WHERE questionid='$quesID'" ;
if($result2 = $mysqli->query($query2) ) {
if($row2 = $result2->fetch_object() ) {
$option1 = $row2->option1 ;
$option2 = $row2->option2 ;
$option3 = $row2->option3 ;
$option4 = $row2->option4 ;
?>
<table border="0" cellpadding="0" cellspacing="0">
<? echo "<div id = $row->questionid name = boxs>" ; ?>
<th> <h2> <? echo $i .")" ; ?> </h2> </th>
<th> <h2> <?echo $row->question ;?> </h2> </th>
<tr> <input type="hidden" name="quesID[]" id="<? echo $quesID ; ?>" value="<? echo $quesID ; ?>">
<div id = "options">
<td>
<span id = "1">
<img id = "image" height="50px" width="50px">
</td>
<td>
<label>
<? echo $option1 ;?>
</label>
</td>
</span>
<td>
<span id = "2">
<img id = "image" height="50px" width="50px">
</td>
<td>
<label>
<? echo $option2 ;?>
</label>
</td>
</span>
<td>
<span id = "3">
<img id = "image" height="50px" width="50px">
</td>
<td>
<label>
<? echo $option3 ;?>
</label>
</td>
</span>
<td>
<span id = "4">
<img id = "image" height="50px" width="50px">
</td>
<td>
<label>
<? echo $option4 ;?>
</label>
</td>
</div>
</span>
</div>
</tr>
</table>
</section>
<?
}
else
echo "No options available for this question" ;
}
}
?>
<script type="text/javascript">
var i = 0
for(i=1; i <= 4; i++) {
var y = document.getElementById(i);
//alert(y.childNodes)
var x = document.getElementById("image");
if( y.id == (<?echo $answer ; ?>) ) {
x.src = "correct.png" ;
}
else
x.src = "wrong.jpg" ;
}
}
</script>
<?
}
?>