提交答案后,同一页面中的小测验显示答案正确还是不正确

时间:2019-07-13 03:20:00

标签: php ajax

“我正在做一个测验,我希望它告诉我答案是正确还是不正确。我的问题是这样做,但是它告诉我在下一个问题的下一页中是正确还是不正确因为每次我提交答案时都会刷新页面。”我希望它在移至下一页之前,在同一页面上告诉学生是正确还是错误。 如果我消除了ajax,它将显示答案是正确还是不正确,但是会在显示新问题的下一页中显示。所有代码都在同一Php / html页面中。预先感谢。

<?php while($row5 = mysqli_fetch_assoc($res)): ;{ ?>
    <h1 align="center" ><?php echo $row5['Question'] ?></h1>
    <?php 
    $id = $row5['QuizzId'];
    $question = $row5['Question'];
    $correctAnswer=$row5['correctAnswer'];
    $ans_array = array($row5['notCorrect1'],$row5['notCorrect2'], $row5['correctAnswer']);
    shuffle($ans_array);
?>
    <label>A<input type="radio" value="<?php echo $ans_array[0]; ?>" name="name"> <?php echo $ans_array[0]; ?></label>
    <br/>
    <br/>
    <label>B<input type="radio" value="<?php echo $ans_array[1]; ?>" name="name"> <?php echo $ans_array[1]; ?></label>
    <br/>
    <br/>
    <label>C <input type="radio" value="<?php echo $ans_array[2]; ?>" name="name"> <?php echo $ans_array[2]; ?></label>
    <input type="hidden" name="correctAn" value="<?php echo $correctAnswer; ?>">
<?php } ?>
<br />
<br />
<input type="submit" value="Submit" name="correct">
<script>
$('#correct').click(function() {
    $.ajax({
        type: 'POST',
        url: 'questions_1.php',
        data: 'correctAnswer1',
        success: function(php) {
            alert('yes');
        }               
    });
});
</script>
<?php
if($_GET["correctAnswer1"]){
    $mianswer = $_POST["name"];
    $correctAn = $_POST["correctAn"];
    if($mianswer==$correctAn) {
        echo "<h1>Great Job</h1>";
        echo '<br>';
        echo "<h1>The correct answer was $correctAn</h1>";
    } else {
        echo '<h1>Not correct</h1>';
        echo "<h1>The correct answer was $correctAn</h1>";
    }
}
?>

2 个答案:

答案 0 :(得分:2)

首先在您的ajax请求中,您已经定义了type:'POST',但是您已经在php中使用了$_GET,请进行以下更改,即:if($_POST['something'])

然后您需要获取radio button的值,该值由用户和correct answer value选择。即:

 $("input[name='correct']").click(function(e) {
     e.preventDefault();
    //getting selected radio button value
    var name=$("input[name='name']:checked").val();
   //getting value of correct answer
    var correctAn=$("input[name='correctAn']").val();
        $.ajax({
            type: 'POST',
            url: 'questions_1.php',
             data: {'correctAn': correctAn,'name':name },//<-passing value to php
            success: function(php) {
                alert(php);
            }               
        });

    });

然后在您的php script中,如下所示:

if(isset($_POST['correctAn']) && isset($_POST['name'])){

   //getting values
        $mianswer=$_POST["name"];
        $correctAn=$_POST["correctAn"];
        //do something

}

答案 1 :(得分:0)

由于您将正确答案作为隐藏字段传递,因此无需进行Ajax调用,因此可以管理客户端所需的内容。如果这是正确的方法,会令人怀疑,因为检查页面会显示正确的答案,但是根据您当前正在做的事情,这是有可能的。

$('form').on('submit', function(ev) {
    ev.preventDefault(); // This prevents form submission
    let answer = $("input[name='name']:checked").val();
    let correct = $("input[name='correctAn']").val();
    if (answer == correct) { // Check if selected option is the correct one
        alert('Congratulations!');
    }
});