[英]PHP Submit multiple questions in a quiz
我想用動態數量的問題創建一個html測驗,每個問題都有動態數量的答案。 我的目標是在測驗完成后提交所有問題。 我需要對HMTL進行哪些更改,以及使用什么php來獲取陣列?
的HTML
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions[]" type="hidden" value="1">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions[]" type="hidden" value="2">
<input name="answers[]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
的PHP
$questionID = $_POST['questions'];
$answers = $_POST['answers'];
foreach( $questionID as $i => $qid ) {
echo "The question id is ".$qid." and selected answer(s) is ".$answers[$i]. "<br>";
}
我相信我的html問題之一與答案有關,兩個問題的名稱相同。 另一個PHP問題是選擇了哪個答案。 我不想使用AJAX,因為這會導致服務器上的負載增加,因此我將JS用於測驗計時器。 謝謝你的幫助。
您應該在輸入名稱屬性中指定問題的編號,例如:
<form action="process.php" method="post">
<div class="questions">
<strong class="white">Question 1</strong>
<input name="questions" type="hidden" value="1">
<input name="answers[1]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[1]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[1]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[1]" type="checkbox" value="d"> <label>answer 4</label><br>
</div>
<div class="questions">
<strong class="white">Question 2</strong>
<input name="questions" type="hidden" value="2">
<input name="answers[2]" type="checkbox" value="a"> <label>answer 1</label><br>
<input name="answers[2]" type="checkbox" value="b"> <label>answer 2</label><br>
<input name="answers[2]" type="checkbox" value="c"> <label>answer 3</label><br>
<input name="answers[2]" type="checkbox" value="d"> <label>answer 4</label><br>
<input name="answers[2]" type="checkbox" value="e"> <label>answer 5</label><br>
<input name="answers[2]" type="checkbox" value="f"> <label>answer 6</label><br>
</div>
</form>
然后,當您獲得$ _POST時,它將像:
答案[1] ='a',答案[2] ='b'...。
我認為這將滿足您的要求:
$questionAnswers = array('question1'=>array('answer1','answer2','answer3','answer4'));
$questionText = array('question1'=>'Question 1');
顯示表格:
<form>
<?php foreach($questionAnswers as $q => $answers)
{
echo '<div class="questions"><strong class="white">'.$questionText[$q].'</strong>'
for($i = 0; $i < count($answers); $i++)
{
echo '<input name="'.$q.'[]" type="checkbox" value="'.$i.'"> <label>'.$answers[$i].'</label><br>';
}
echo '</div>';
}
?>
</form>
處理提交:
$foreach($questionAnswers as $q => $answers)
{
$userAnswers = isset($_REQUEST[$q]) ? $_REQUEST[$q] : array();
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.