2017-02-13 25 views
0

私はPHPを使って非常に単純なクイズを実装しようとしていますが、私は他のコピーの例を探すのに苦労しています。私はさまざまなソリューションに対応していますが、要件は次のとおりです。テキストボックス入力を使った単純なPHPクイズ

  • クイズは1ページで実行されます。
  • 一度に1つの質問が表示され、ユーザーは現在の質問に正しく回答したときに次の質問のみを表示できます。
  • すべての質問タイプは「短い回答」で、テキストボックスに正解を入力する必要があります。

私は、ユーザーの回答に基づいてPHPの条件文(/他の場合)を使用して、解決策を見つけようとしていると私の現在のコードは次のようになります。

<form method="POST" action=""> 
<label for="question01">1. How many sides does a triangle have?</label> 
<input type="text" name="guess01" value=""> 
<input type="submit"> 
</form> 
<?php 
// Process Question 1 Form 
$guess01 = $_POST['guess01']; 
$guess01 = strtolower($guess01); 
$answer01 = "three"; 

if ($guess01 == $answer01) 
{ 
?> 
<p>Correct answer given for question 1!</p> 
<form method="POST" action=""> 
<label for="question02">2. How many sides does a square have?</label> 
<input type="text" name="question02" value="" placeholder="Answer here..."> 
<input type="submit"> 
</form> 

<?php 
// Process Question 2 Form 
$guess02 = $_POST['guess02']; 
$guess02 = strtolower($guess02); 
$answer02 = "four"; 

if ($guess02 == $answer02) 
{ 
?> 
<p>Correct answer given for question 2!</p> 
<form> 
<label for="question02">3. How many sides does a pentagon have?</label> 
<input type="text" name="question02" value="" placeholder="Answer here..."> 
<input type="submit"> 
</form> 

<?php 
// Process Question 2 Form 
$guess03 = $_POST['guess02']; 
$guess03 = strtolower($guess02); 
$answer03 = "five"; 

if ($guess03 == $answer03) 
{ 
?> 
<p>Correct answer given for question 3!</p> 
<p>Quiz completed!</p> 

<?php 
} 
elseif (empty($guess03)) 
{ 
echo "<!-- No Answer Given -->"; 
} 
else 
{ 
echo "<p>Try Again!</p>"; 
} 
?> 

<?php 
} 
elseif (empty($guess02)) 
{ 
echo "<!-- No Answer Given -->"; 
} 
else 
{ 
echo "<p>Try Again!</p>"; 
} 
?> 

<?php 
} 
elseif (empty($guess01)) 
{ 
echo "<!-- No Answer Given -->"; 
} 
else 
{ 
echo "<p>Try Again!</p>"; 
} 
?> 

私は、これは非常にあると確信しています非効率的なコーディングと私はこれに取り組む他の方法に開放されています。どんな助力も非常に感謝して受け取ります。ありがとうございました。

答えて

0

質問のフォームと検証は常にデータが変わるだけなので、コードをコピーする必要はありません。質問と回答を配列に入れ、現在の質問の番号をフォームに入力するだけです。

// Declare all your questions and answers as multi dimensional array 
$questionsAndAnwsers = array(array("question" => "1. How many sides does a triangle have?", "answer" => "three"), 
          array("question" => "2. How many sides does a square have?", "answer" => "four")); 

// current question 
$currentQuestion = 0; 
if(isset($_POST["currentQuestion"])){ 
    $currentQuestion = $_POST["currentQuestion"]; 
    if($_POST["guess"] == $questionsAndAnwsers[$currentQuestion]["answer"]){ 
     // answer was correct, increment your question-counter for the next question 
     $currentQuestion++; 
     // print success message 
     } else { 
     // question was wrong, counter stays as it is, so same question will be printed 
     // print failure message 
     } 
} 


?> 
<form method="POST" action=""> 
    <label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label> 
    <input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>"> 
    <input type="text" name="guess" value=""> 
    <input type="submit"> 
</form> 
<?php 

すべての質問に回答した場合、追加のif節を追加する必要があります。

編集: これは簡単な実装であり、不正行為は簡単です。質問番号がブラウザに渡されると、ユーザーはそれを変更できます(9に設定)。この全体を閉じるには、セッションを使用して、サーバー上のセッションで最後の正解の番号を保存する必要があります。

+0

ありがとうございます。それは非常にうまく動作します。 あなたが言っていたと思うように、フォームは隠された入力の値を変更することによって不正行為を起こすことがあります。しかし、私のテストでは、これは、完了した条件(フォーム自体を含む新しいif/elseステートメント)を実際に満たしていない、後の質問のみを引き起こす可能性があります。 – langton82

関連する問題