我需要用真/假答案来构建问题论文。每个问题都有5个答案。比如a,b,c,d和e。
在提交之前,学生单击每个答案的复选框。当我最后点击提交按钮时,我需要发送数据库表的全部答案。
请让我知道,如何保留价值直到提交。
我的示例代码,
<script>
function sendReq(x,y)
{
var answer_id = $("input[name=answer]").val();
$.ajax({
type: 'POST',
url: 'test_submit.php',
data: { qid: x, pid: y,aid: answer_id },
success: function(data) {
$("#answer_response").html(data);
}
});
}
</script>
<script>
function clickCheckbox(){
var checkedValue = null;
var inputElements = document.getElementsByClassName('messageCheckbox');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValue = inputElements[i].value;
$("#answer").val(checkedValue);
break;
}
else
{
$("#answer").val(null);
}
}
}
</script>
<ul class="list6">
<?php
include('config/config2.php');
$rec_limit = 1;
$query = "SELECT count(question_id) FROM question WHERE paper_id='".$paperid."'";
$result = mysqli_query($con,$query);
if(! $result )
{
die('Could not get data: ' . mysqli_error());
}
$row = mysqli_fetch_array($result, MYSQLI_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
if( isset($_GET{'prnt'} ) )
{
if($rec_count>(($page+1)* $rec_limit))
$prnt=($page+1)* $rec_limit." of ".$rec_count;
else
$prnt=$rec_count." of ".$rec_count;
}
else
{
if($rec_count>$rec_limit)
$prnt=$rec_limit." of ".$rec_count;
else
$prnt=$rec_count." of ".$rec_count;
}
$query = "SELECT * FROM question WHERE paper_id='".$paperid."' LIMIT $offset, $rec_limit";
$result = mysqli_query($con, $query );
if(! $result )
{
die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($result))
{
echo "<form action='' method='post' onsubmit='myButton.disabled = true; return true;'>";
$result2 = mysqli_query($con,"SELECT * FROM answer");
if($row['paper_id'] == $paperid){
echo '<li>' .$row['question'].'</li>';
$question_id;
$paper_id;
while($row2 = mysqli_fetch_array($result2))
{
if($row['question_id'] == $row2['question_id']){
$question_id = $row['question_id'];
$scheme = $row['scheme_id'];
$query_scheme = "SELECT * FROM mark_scheme WHERE scheme_id='".$scheme."'";
$result_scheme = mysqli_query($con, $query_scheme );
$row_scheme = mysqli_fetch_array($result_scheme);
if($row_scheme['type_id'] == 1){
$answer_id = $row2['answer_id'];
$paper_id = $row2['paper_id'];
echo '<li><input type="radio" id="answer_id_group" name="answer_id" value="'.$answer_id.'" onchange="getRadioValue();">'.$row2['answer'].'</li>';
}
else {
$answer_id = $row2['answer_id'];
$paper_id = $row2['paper_id'];
echo '<li><input type="checkbox" id="answer_id_group" class="messageCheckbox" name="chb[]" value="1" onclick="clickCheckbox()">True<input type="checkbox" id="answer_id_group" class="messageCheckbox" name="chb[]" value="0" onclick="clickCheckbox()">False <input type="hidden" id="answer_id_group" name="chb[]" value="'.$answer_id.'">'.$row2['answer'].'</li>';
}
}
}
echo '<input type="hidden" id="email_group" name="email" value="'.$email.'">';
echo '<input type="hidden" id="paper_id" name="paper_id" value="'.$paper_id.'">';
echo '<input type="hidden" id="question_id" name="question_id" value="'.$question_id.'">';
echo '<input type="hidden" id="question_id_group" name="question_id" value="'.$row['question_id'].'">';
echo '<input type="hidden" id="answer" name="answer" value="">';
echo '<input type="hidden" id="paper_id_group" name="paper_id" value="'.$row['paper_id'].'">';
$already_submitted = mysqli_query($con,"SELECT * FROM student_answer WHERE user='".$email."' AND question_id='".$question_id."' AND type=1");
if(mysqli_num_rows($already_submitted))
{
echo '<style>#submit_answer{ display: none;}</style>';
}else{
}
echo '<li><input type="submit" name="myButton" value="Submit" class="user-btn" id="submit_answer" onclick="return sendReq('.$question_id.','.$paper_id.');"></li>';
echo '<br><br>';
}
echo "</form>";
}
if( $page == 0 )
{
echo $prnt." |";
echo "<a href=\"?page=$page&prnt=$prnt\">"; echo "Next >>"; echo"</a>";
$left_rec = $rec_count - ($page * $rec_limit);
if( isset($_GET{'prnt'} ) )
{
if($rec_count>(($page+1)* $rec_limit))
$prnt=($page+1)* $rec_limit." of ".$rec_count;
else
$prnt=$rec_count." of ".$rec_count;
}
else
{
if($rec_count>$rec_limit)
$prnt=$rec_limit." of ".$rec_count;
else
$prnt=$rec_count." of ".$rec_count;
}
$query = "SELECT * FROM question WHERE paper_id='".$paperid."' LIMIT $offset, $rec_limit";
$result = mysqli_query($con, $query );
if(! $result )
{
die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($result))
{
echo "<form action='' method='post' onsubmit='myButton.disabled = true; return true;'>";
$result2 = mysqli_query($con,"SELECT * FROM answer");
if($row['paper_id'] == $paperid){
echo '<li>' .$row['question'].'</li>';
$question_id;
$paper_id;
while($row2 = mysqli_fetch_array($result2))
{
if($row['question_id'] == $row2['question_id']){
$question_id = $row['question_id'];
$scheme = $row['scheme_id'];
$query_scheme = "SELECT * FROM mark_scheme WHERE scheme_id='".$scheme."'";
$result_scheme = mysqli_query($con, $query_scheme );
$row_scheme = mysqli_fetch_array($result_scheme);
if($row_scheme['type_id'] == 1){
$answer_id = $row2['answer_id'];
$paper_id = $row2['paper_id'];
echo '<li><input type="radio" id="answer_id_group" name="answer_id" value="'.$answer_id.'" onchange="getRadioValue();">'.$row2['answer'].'</li>';
}
else {
$answer_id = $row2['answer_id'];
$paper_id = $row2['paper_id'];
echo '<li><input type="checkbox" id="answer_id_group" class="messageCheckbox" name="chb[]" value="1" onclick="clickCheckbox()">True<input type="checkbox" id="answer_id_group" class="messageCheckbox" name="chb[]" value="0" onclick="clickCheckbox()">False <input type="hidden" id="answer_id_group" name="chb[]" value="'.$answer_id.'">'.$row2['answer'].'</li>';
}
}
}
echo '<input type="hidden" id="email_group" name="email" value="'.$email.'">';
echo '<input type="hidden" id="paper_id" name="paper_id" value="'.$paper_id.'">';
echo '<input type="hidden" id="question_id" name="question_id" value="'.$question_id.'">';
echo '<input type="hidden" id="question_id_group" name="question_id" value="'.$row['question_id'].'">';
echo '<input type="hidden" id="answer" name="answer" value="">';
echo '<input type="hidden" id="paper_id_group" name="paper_id" value="'.$row['paper_id'].'">';
$already_submitted = mysqli_query($con,"SELECT * FROM student_answer WHERE user='".$email."' AND question_id='".$question_id."' AND type=1");
if(mysqli_num_rows($already_submitted))
{
echo '<style>#submit_answer{ display: none;}</style>';
}else{
}
echo '<li><input type="submit" name="myButton" value="Submit" class="user-btn" id="submit_answer" onclick="return sendReq('.$question_id.','.$paper_id.');"></li>';
echo '<br><br>';
}
echo "</form>";
}
if( $page == 0 )
{
echo $prnt." |";
echo "<a href=\"?page=$page&prnt=$prnt\">"; echo "Next >>"; echo"</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"?page=$last&prnt=$prnt\"> "; echo "<< Last"; echo"</a>";
}
else if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"?page=$last&prnt=$prnt\">"; echo "<< Last"; echo"</a> |";
echo " ".$prnt." |";
echo "<a href=\"?page=$page&prnt=$prnt\">"; echo "Next >>"; echo"</a>";
}
?>
</ul>
<div id="answer_response" class="answer-box"></div>
拜托,任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
如果有多个复选框,那么你不能使用复选框的id,因为id只会考虑第一个元素来获取所有选中的值使用class。为所有复选框指定相同的类名,并使用以下选择器
获取所选复选框或单选按钮值$("input[name=answer]:checked").val()