【问题标题】:how do i display correct and incorrect answer when user click on radio buttons用户单击单选按钮时如何显示正确和错误的答案
【发布时间】:2017-06-08 18:42:01
【问题描述】:

我正在开发一个小型测验网络应用程序,我想在用户单击单选按钮时显示正确或错误的答案。

问题是Ajax在AnswerResult Column Water中显示了不正确的选项,并且正确和错误。我希望无论何时如果和正确,函数应该在 AnswerResult 列中正确显示,如果和错误,函数应该在 AnswerResult 列中显示不正确

HTML 表单

<div id='question<?php echo $i;?>' class='cont'>
  <h3>Question No  <?php echo $i?></h3><br>
  <p class='questions' id="qname<?php echo $i;?>"><?php echo $result['question'];?></p>

  <h3>Choose Answer:</h3><br>
    <input type="radio" value="A" class='radioButton radioButton<?php echo $result['que_id'];?>' name='<?php echo $result['que_id'];?>'/><?php echo $result['optionA'];?> <br/>
    <input type="radio" value="B" class='radioButton radioButton<?php echo $result['que_id'];?>' name='<?php echo $result['que_id'];?>'/><?php echo $result['optionB'];?><br/>
    <input type="radio" value="C" class='radioButton radioButton<?php echo $result['que_id'];?>' name='<?php echo $result['que_id'];?>'/><?php echo $result['optionC'];?><br/>
    <input type="radio" value="D" class='radioButton radioButton<?php echo $result['que_id'];?>' name='<?php echo $result['que_id'];?>'/><?php echo $result['optionD'];?><br/>
    <input type="radio" checked='checked' style='display:none' value="5" class='radioButton radioButton<?php echo $result['que_id'];?>' name='<?php echo $result['que_id'];?>'/><br/>
    <button disabled="disabled" id='<?php echo $i;?>' class='mc-btn btn-style-6 nextButton<?php echo $result['que_id'];?>' type='button'>Next</button>
</div>     

<h4 class="title-sb sm bold">

  Correct Answer:
  <span class="AnswerResult"></span>

Ajax 请求检查答案

   $('body').on('click', '.radioButton', function(){

var selected_answer = $(this).val();
var question_id = $(this).attr('name');

postData = 'ajaxrequest=submit_question&question_id='+question_id+'&selected_answer='+selected_answer;

 $.ajax
 ({
   data: postData,
   type:'POST',
   url: "start-quiz.php",
   dataType : 'html',
   cache : false,


   success: function(data) {
        if(data.correct == 'true')
        {   
            $('.AnswerResult').html("Correct");
            console.log('correct');
        }else {
            $('.AnswerResult').html("Incorrect");
            console.log('incorrect');
        }
            $('input.radioButton'+question_id).disabled(true);
            $('.nextButton'+question_id).disabled(false);

         }
        });
       });

Php Sql Query 根据 Ajax 请求获取答案

if(isset($_REQUEST['ajaxrequest']) && $_REQUEST['ajaxrequest'])
{
    $return_data = array();
    switch(strtolower($_REQUEST['ajaxrequest']))
    {

        case 'submit_question':

            $question_id = (int)$_POST['question_id'];
            $sel_answer = $_POST['selected_answer'];
            $return_data['correct'] = false;

            // Check database if answer is correct or wrong
            $ans = mysql_query("select rightans from  question_bank where que_id='$question_id'")or die("select  correct  ans");
            //$getans=mysql_query($ans) ;
            $showans=mysql_fetch_row($ans);
            $db_answer = $showans['rightans']; 

            if($sel_answer == $db_answer)
                $return_data['correct'] = true;

            break;

    }

    echo json_encode($return_data);
    die();
}

Ajax 请求发布时问题 AnswerResult 列显示不正确

【问题讨论】:

  • 为什么你删除了昨天的问题,又为同一个问题打开了一个新的问题?
  • 你有&lt;span class="AnswerResult"&gt;&lt;/span&gt;;所以你需要$('.AnswerResult').html( 而不是$('#AnswerResult').html( 因为你有class 而不是id
  • 您的 ajax 也存在问题:dataType:'html' 但从后端返回的 JSON 对象...!!!
  • 在成功处理程序的第一行执行console.log(data)
  • MySql 已弃用尝试使用 mysqli

标签: php jquery ajax database


【解决方案1】:

编辑更正了 ajax 函数。

$(document).on('change', '.radioButton', function(){
  var selected_answer = $(this).val();
  var question_id = $(this).attr('name');

  postData = 'ajaxrequest=submit_question&question_id='+question_id+'&selected_answer='+selected_answer;

  $.ajax
  ({
    url: "start-quiz.php",
    data: postData,
    type: 'POST',
    dataType: 'json',
    processData: false,
    contentType: 'application/x-www-form-urlencoded',
    cache : false,
    success: function(data) {
      console.log(data);
      if (typeof (data.correct) !== 'undefined' && (data.correct != "" || data.Key != null)){
      var response = (data.correct == 'true')?"Correct":"Incorrect";
      //(data.correct == true)?"Correct":"Incorrect"
      $('.AnswerResult').html(response);
      $('input.radioButton'+question_id).disabled(true);  
      $('.nextButton'+question_id).disabled(false);
     }



    }
  });
 });

【讨论】:

  • 兄弟&lt;span class="AnswerResult"&gt;&lt;/span&gt;还是空的
  • soni vimal 你有Skype id吗?
  • 我有 TeamViewer... 你有吗?
  • 通过 TeamViewer 将您的 PC 作为遥控器,将能够解决您的问题。那你能下载安装吗?
【解决方案2】:

请更改 ajax 调用中的数据类型:

来自

dataType : 'html',

dataType : 'json',

然后试试 console.log(data[0]);

看看你会得到什么。

【讨论】:

  • 兄弟,我尝试了 Soni Vimal ajax 函数,但仍然是同样的问题
  • 你在data[0]中得到了什么数据
猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
相关资源
最近更新 更多