简体   繁体   English

使用单选按钮获取正确答案-在线测验

[英]Getting the correct answer using radiobutton - Online Quiz

I'm doing a Online Quiz using php and I want to get the correct answer id if the radio button is checked and also I have a dynamic textbox which are the options in question. 我正在使用php进行在线测验,如果选中了单选按钮,并且想获得一个正确的动态文本框,则希望获得正确的答案ID。 Here's the sample photo 这是样本照片

在此处输入图片说明 In my database, I have a two tables which are the e_question and e_answer . 在我的数据库中,我有两个表,分别是e_questione_answer This program can perform a insert query into question and answers table but I need to update the question table to insert the correct answer. 该程序可以对问题和答案表执行插入查询,但是我需要更新问题表以插入正确的答案。 Here's my sample table: 这是我的示例表:

e_question and e_answer Table e_questione_answer

在此处输入图片说明

The value of answer_id is 0 because I can't get the id of correct answer. answer_id的值为0,因为我无法获得正确答案的ID。

在此处输入图片说明 Here's my design code: 这是我的设计代码:

  <div id="container">
    <div class="form-group">
      <label class="control-label col-sm-2">Answer options :</label>
      <div class="col-sm-2" style="margin-right: -120px;">
       <input type="radio" name="correct" style="display: inline; position: absolute; margin-top: 10px;">
      </div>
      <div class="col-sm-8" >
       <input type="text" class="form-control" name="ans[]" placeholder="Answer">
      </div>
    </div>

    <div class="form-group">
      <label class="control-label col-sm-2"></label>
      <div class="col-sm-2" style="margin-right: -120px;">
       <input type="radio" name="correct" style="display: inline; position: absolute; margin-top: 10px;">
      </div>
      <div class="col-sm-8" >
       <input type="text" class="form-control" name="ans[]" placeholder="Answer">
      </div>
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-sm-2"></label>
    <div class="col-sm-6" >
      <a href="#" id="add"><i class="fa fa-fw fa-md fa-plus-circle"></i> Add answer option</a>
    </div>
  </div>

And my jquery for dynamic textbox 而我的动态文本框的jQuery

<script type="text/javascript">
  $(document).ready(function(){
    var addOption = '<div><div class="form-group"><label class="control-label col-sm-2"></label><div class="col-sm-2" style="margin-right: -120px;"><input type="radio" name="correct" style="display: inline; position: absolute; margin-top: 10px;"></div><div class="col-sm-8" ><input type="text" class="form-control" name="ans[]" placeholder="Answer"></div><a href="#" id="remove"><i class="fa fa-fw fa-md fa-close" style="color:red" ></i></a></div></div>';

    var maxRows = 3;
    var x = 1;

      $("#add").click(function(e){
        if (x <= maxRows) {
          $("#container").append(addOption);
          x++;
        }
      });

      $("#container").on('click','#remove', function(e){
          $(this).parent('div').remove();
          x--;
      });

  });
</script>

Please help me. 请帮我。 Thank you in advance! 先感谢您!

In e_answer table you have add one more column like correct answer correct_answer (enum - 0,1). e_answer表中,您又添加了一列,例如正确答案correct_answer (枚举correct_answer )。

Because database should be aware about correct answer. 因为数据库应该知道正确的答案。 So, What will happen? 那么,会发生什么呢?

Suppose Our e_answer table like below: 假设我们的e_answer表如下:

answerid (13, 14, 15, 16)
questionId(10, 10, 10, 10)
Option (demo1, demo2, demo3, demo4)
correct_answer (0,0,1,0)

That means, In question no. 那意味着,问题不。 10 has 4 options and "Demo3" is correct answer because we have added '1' for that options. 10有4个选项,“ Demo3”是正确答案,因为我们为该选项添加了“ 1”。

So, we can check like, If user will choose demo3 then it will be correct answer. 因此,我们可以检查一下,如果用户选择demo3那么它将是正确的答案。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM