有什么方法可以在表的字段中插入多个数据,而一个特定的数据在其字段中具有不同的值?点火枪

时间:2020-03-12 03:51:17

标签: javascript php mysql ajax codeigniter

我正在制作测验应用程序,一个问题必须正确。我已经可以在表中插入多个数据,但问题是,我有一个特定的选择或答案,该选择必须与其他选择或答案不同。

该应用的屏幕截图以及我打算做什么

enter image description here

这是我的桌子的样子

table answers

id | answer | correct
 1 |  Apple |     1
 2 |  Mango |     0
 3 |  Melon |     0

1,表示正确答案,0表示错误。

这是我的模特。

我尝试尝试获取单选按钮值(即 1)并将其插入数据库中,但结果是,如果我添加其他数据或多个数据。索引0或第一个数据是唯一可以插入的数据。我不能选择我可以检查的单选按钮仅我可以检查的第一按钮

// Insert questions

    $field_question = array(
        'question'=>$this->input->post('question'),
    );

    $this->db->insert('questions', $field_question);

    // Insert Answers
    $data_answer = $this->input->post('choice[]');
    $data_is_correct = $this->input->post('checkChoice[]');


    $value = array();

    for($i = 0; $i < count($data_answer); $i++) {
        $value[$i] = array(
            'answer' => $data_answer[$i],
            'correct' => $data_is_correct[$i],
            'question_id' => $this->db->insert_id(),
        );
    }


    $this->db->insert_batch('answers', $value);

    if($this->db->affected_rows() > 0){
        return true;
    }else{
        return false;
    }

如果我添加3个新数据,表的问题

id | answer | correct
 1 |  Apple |     1
 2 |  Mango |     0
 3 |  Melon |     0
* New 3 Data Inserted 
 4 | Orange |     1
 5 | Tomato |     0
 6 | Grapes |     0

我不能用TomatoGrapes来回答问题,也不能将其设为值1(仅橙色)或第一个添加的数据。

查看

这是我的单选按钮

<div class="form-check">
 <input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" checked  />
    <label class="form-check-label" for="exampleRadios1">
        Make this as an Answer
    </label>
</div>

还有我的表格。

<form method="post" id="myForm" action="<?php echo base_url(); ?>posts/addQuestion">
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <span class="input-group-text">Question</span>
                        </div>
                        <input type="text" placeholder="Enter your Question" name="question" id="question" class="form-control" required />
                    </div>

                    <hr>
                    <h5>Create Choices: </h5>
                    <div class="input-group input-group-sm mb-3">
                    <div class="table-responsive">  
                               <table class="table table-bordered" id="dynamic_field">  
                                    <tr>  

                                         <td><input type="hidden" name="choiceHid[]" value="0" /></td> 
                                         <td><input type="text" name="choice[]" id="choice" placeholder="Enter your Choice" class="form-control" /> </td>  
                                         <td><button type="button" name="add" id="add" class="btn btn-success"><span class="iconify" data-icon="ant-design:plus-circle-outlined" data-inline="false"></span> Add Response </button></td>  
                                         <td>
                                            <div class="form-check">
                                                <input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" checked  />
                                                    <label class="form-check-label" for="exampleRadios1">
                                                        Make this as an Answer
                                                    </label>
                                            </div>
                                        </td>
                                    </tr>  

                               </table>  
                          </div>  
                    </div>

                    <hr>

                    <input type="button" id="btnSave" class="btn btn-block btn-info" value="Submit" />

</form>

脚本

<script>

    $('#btnSave').click(function(){
            var url = $('#myForm').attr('action');
            var data = $('#myForm').serialize();
            //validate form

                $.ajax({
                    type: 'ajax',
                    method: 'post',
                    url: url,
                    data: data,
                    async: false,
                    dataType: 'json',
                    success: function(response){
                        if(response.success){
                            $('#myForm')[0].reset();
                            if(response.type=='add'){
                                var type = 'added'
                            }
                            swal("Success!", "You created a Question!", "success");

                        }else{
                            alert('Error');
                        }
                    },
                    error: function(){
                        alert('Could not add data');
                    }
                });
        });


</script>

动态字段脚本

<script>  
 $(document).ready(function(){  
      var i=1;  
      $('#add').click(function(){  
           i++;  
           $('#dynamic_field').append(
               '<tr id="row'+i+'">'+
                 '<td><input type="hidden" name="choiceHid[]" value="0" /></td>'+
                 '<td><input type="text" name="choice[]" placeholder="Enter your Choice" class="form-control" /></td>'+
                 '<td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><span class="iconify" data-icon="dashicons:remove" data-inline="false"></span></button></td>'+
                 '<td>'+
                        '<div class="form-check">'+
                            '<input class="form-check-input" type="radio" name="checkChoice[]" id="checkChoice" value="1" />'+
                                    '<label class="form-check-label" for="exampleRadios1">'+
                                        'Make this as an Answer'+
                                    '</label>'+
                        '</div>'+
                 '</td>'+
               '</tr>'
               );  

      });  

      $(document).on('click', '.btn_remove', function(){  
           var button_id = $(this).attr("id");   
           $('#row'+button_id+'').remove();  
      });  

 });  
 </script>

1 个答案:

答案 0 :(得分:0)

动态字段脚本

将变量 val 设为checkChoice的值,如下所示...

  <script>  
     $(document).ready(function(){  
          var i=1;  
          $('#add').click(function(){  
               var val =i;
               i++;  
               $('#dynamic_field').append(
                   '<tr id="row'+i+'">'+
                     '<td><input type="hidden" name="choiceHid[]" value="0" /></td>'+
                     '<td><input type="text" name="choice[]" placeholder="Enter your Choice" class="form-control" /></td>'+
                     '<td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><span class="iconify" data-icon="dashicons:remove" data-inline="false"></span></button></td>'+
                     '<td>'+
                            '<div class="form-check">'+
                                '<input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="'+val+'" />'+
                                        '<label class="form-check-label" for="exampleRadios1">'+
                                            'Make this as an Answer'+
                                        '</label>'+
                            '</div>'+
                     '</td>'+
                   '</tr>'
                   );  

          });  

          $(document).on('click', '.btn_remove', function(){  
               var button_id = $(this).attr("id");   
               $('#row'+button_id+'').remove();  
          });  

     });  
     </script>

查看

如下所示将checkChoice的初始值指定为0

<div class="form-check">
 <input class="form-check-input" type="radio" name="checkChoice" id="checkChoice" value="0" checked  />
    <label class="form-check-label" for="exampleRadios1">
        Make this as an Answer
    </label>
</div>

//插入答案

$data_answer = $this->input->post('choice[]');
$data_is_correct = $this->input->post('checkChoice');


$value = array();

for($i = 0; $i < count($data_answer); $i++) {

          if($data_is_correct == $i) {
            $correct = 1;
          } else {
            $correct = 0;
          }
    $value[$i] = array(
        'answer' => $data_answer[$i],
        'correct' => $correct,
        'question_id' => $this->db->insert_id(),
    );
}