2016-05-02 72 views
-1

我想做出多項選擇,如果我們選擇了錯誤的答案,那麼文本'不正確'將顯示在我們選擇的答案的一邊,如果我們選擇正確的答案,那麼文本'正確'將顯示在我們選擇的答案的一側。我使用單選按鈕。請幫忙。如何在答案中做出多項選擇的結果

這是我的index.php:

<?php 

    include 'koneksi.php'; 
    session_start(); 
    $query = mysql_query("select * from t_soal") or die (mysql_error()); 
    //$_SESSION['soal'] = mysql_fetch_array($query); 
    $_SESSION['soal'] = array(); 
    $_SESSION['no'] = 1; 
    $_SESSION['score'] = 0; 
    $_SESSION['option'] = array(); 
    $_SESSION['jawab'] = array(); 
    $i=0; 
    while($row = mysql_fetch_assoc($query)){ 
     $_SESSION['soal'][] = $row; 
     $_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']); 
     $i++; 
    } 

    if(isset($_SESSION['soal'])){ 
     header("location:test.php"); 
    } 

這是test.php的:

<?php 
    session_start(); 
    $soal = $_SESSION['soal']; 
    $no = $_SESSION['no']; 
    if(isset($_POST['next'])){ 
     $_SESSION['jawab'][] = $_POST['option']; 
     if($_POST['option'] == $soal[$no-2]['kunci']){ 
      $_SESSION['score'] = $_SESSION['score'] + 10; 
     } 
    } 
    if(isset($soal[$no-1])){ 
?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title>Latihan Soal</title> 
</head> 
<body> 
<a href="index.php">Kembali ke soal 1</a> 
    <form action="" method="POST"> 
     <p> 
     <?php 
      echo $no.". "; $_SESSION['no']++; 
      echo $soal[$no-1]['soal']; 
      $jawaban = $_SESSION['option'][$no-1]; 
      shuffle($jawaban); 
     ?> 
     </p> 
     <?php 
      for ($i=0; $i < 4; $i++) { 
     ?> 
      <input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br> 
     <?php 
      } 
     ?> 
     <input type="submit" name="next" value="next"> 
    </form> 
</body> 
</html> 
<?php 
    }else{ 
     header("location:result.php"); 
    } 
?> 
+1

它看起來並不像你已經努力編寫你想要的東西。你開始這個過程,然後尋求幫助,如果你卡住了。 ** SO不是免費的編碼服務** – RiggsFolly

回答

0

假設所有的腳本已經工作,則:

在PHP:

您需要爲此腳本分配一個新參數:

if($_POST['option'] == $soal[$no-2]['kunci']){ 
    $_SESSION['score'] = $_SESSION['score'] + 10; 
} 

if($_POST['option'] == $soal[$no-2]['kunci']){ 
    $_SESSION['score'] = $_SESSION['score'] + 10; 
    $_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this 
} 

,然後從

for ($i=0; $i < 4; $i++) { 
    ?> 
     <input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br> 
    <?php 
     } 

for ($i=0; $i < 4; $i++) { 
    ?> 
    <input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br> 
    <?php 
    echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";  
} 

在JS:(很容易)

HTML文件會看起來像這樣的:

<div class="correctAnswer"> 
    <input type="radio" name="theanswer" id="choice1" /> 
    <label for="choice1">Choice 1</label> 
</div> 
<div class="incorrectAnswer"> 
    <input type="radio" name="theanswer" id="choice2" /> 
    <label for="choice2">Choice 2</label> 
</div> 

和JS會是這樣的:

$(".correctAnswer").click(function() { 
$(".checks").remove(); 
$(this).append("<span class='checks'> correct</span>"); 
}); 
$(".incorrectAnswer").click(function() { 
    $(".checks").remove(); 
    $(this).append("<span class='checks'> incorrect</span>"); 
}); 

如果你想有一個工作示例,請檢查該fiddle link here

希望它有幫助!

+0

theres將是一個錯誤注意:未定義的索引:correctAnswer在C:\ xampp \ htdocs \ aa \ test.php – Endone

相關問題