2017-04-23 57 views
0
question{ 
question_id, 
option1, 
option2, 
option3, 
option4, 
answer 
} 

exam_paper{ 
exam_paper_id, 
exam_paper_name 
} 

exam_question_list{ 
id, 
exam_paper_id, 
question_id 
} 

applicant_do_exam{ 
applicant_do_exam_id, 
exam_paper_id, 
question_id, 
app_answer 
} 

I want to display the questions in exam_paper_id=1 

$query="SELECT * FROM exam_question_list e LEFT JOIN question q ON e.question_id=q.question_id where exam_paper_id='$exid'"; 
$result=mysqli_query($dbcon,$query); 

<form action="exampaper_result.php?id=<?php echo $exid; ?>&stime=<?php echo $stime?>" method="post"> 

<table> 
<!--question_1--> 
<?php 

     $i=1; 
     while($row = mysqli_fetch_array($result)){ 

?> 
    <tr> 


    <tr id="exquestion" data-label="QuestionID"><td><input type="hidden" name="question_id" value="<?php echo $row['question_id']; ?>" /></td></tr> 
    <tr id="exquestion" data-label="Question"><td><span><?php echo $i++; ?>)&nbsp;&nbsp;</span><?php echo $row['question']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer" value="1"/><?php echo $row['option1']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer" value="2"/><?php echo $row['option2']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer" value="3"/><?php echo $row['option3']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer" value="4"/><?php echo $row['option4']; ?></td></tr> 

</tr> 


<?php 
    } 
    ?> 

    </table> 

    <input name="submit" type="submit" id="Submit" value="Submit"/> 

    </form> 

在這裏我可以正確地得到問題和他們的選擇。 但我無法選擇每個問題中的單選按鈕(答案)。 給出的整個頁面僅爲所有問題選擇一個單選按鈕。爲什麼單選按鈕無法正常工作

我的錯誤是什麼?

+0

你的單選按鈕都有相同的ID。這不應該。它們應該都具有相同的名稱,但每個控件都應該有唯一的ID。 – Bindrid

+0

該怎麼做? –

+0

當我選擇問題1單選按鈕(答案)後,我單擊問題2單選按鈕(答案),然後問題1單選按鈕被取消選擇,並選擇問題2單選按鈕 –

回答

0

您需要拆分每個部分的名稱以允許他們在其上下文中進行選擇。例如name="app_answer[<?php echo $i; ?>]"這將爲您提供$_POST['app_answer'] = array()

像這樣:在

while ($row = mysqli_fetch_array($result)) { 
    $g = $row['question_id']; 
?> 
     <tr id="exquestion" data-label="Question"><td><span><?php echo $i++; ?>)&nbsp;&nbsp;</span><?php echo $row['question']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer[<?php echo $g; ?>]" value="1"/><?php echo $row['option1']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer[<?php echo $g; ?>]" value="2"/><?php echo $row['option2']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer[<?php echo $g; ?>]" value="3"/><?php echo $row['option3']; ?></td></tr> 
     <tr data-label="Question"><td><input id="exoption" type="radio" name="app_answer[<?php echo $g; ?>]" value="4"/><?php echo $row['option4']; ?></td></tr> 
<?php } /*end while*/ ?> 

結果提交

<?php 
$answers = (array) $_POST['app_answer']; 
//type-casted to ensure empty value is still a blank array 
foreach ($answers as $question_id => $answer) { 
    echo 'Answer to Question - ' . $question_id . ': ' . $answer . '<br/>'; 
} 
exit; 

您將需要能夠在關鍵$g/question_id解析到你想要的格式。

相關問題