【问题标题】:Is there any way to update and insert selected inputs to the database? Codeigniter有没有办法更新和插入选定的输入到数据库?代码点火器
【发布时间】:2020-03-11 17:06:09
【问题描述】:

我已经成功插入了多个值,现在问题是要查询的,如何同时更新correct_id更新为1 - 如何使用单选按钮作为参考值更新选定的输入。 这是我的 UI,它是如何设计的。

我有一个字段名称correct_id,其值必须为 1 才能被视为正确答案。请彻底指导我,因为这对我来说是一个挑战。我最近对这个查询感到困惑,谢谢!

如您所见,我有单选按钮,我需要选择一个输入或选项作为我的答案

这是我的模型

public function addQuestion(){
    // Insert questions

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

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

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


    $value = array();

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


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

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

还有我的表单在视图中

在这里,我将准确地显示无线电输入及其名称和值

<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>

这是完整的表单代码。

<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">
                                                    <label class="form-check-label" for="exampleRadios1">
                                                        Make this as an Answer
                                                    </label>
                                            </div>
                                        </td>
                                    </tr>  

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

                    <hr>
                    <div class="col-md-8">
                            <h5> Your Answer: 
                            <input type="hidden" name="txtAnswer" value="0" />
                            <input type="text" placeholder="Enter the Question's Answer" name="answer_choice" id="answer_choice" class="form-control mt-2" /><h5>
                    </div>
                    <hr>

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

</form>

【问题讨论】:

    标签: php arrays codeigniter codeigniter-3


    【解决方案1】:

    首先,为每个答案单选和文本字段分配不同的索引随机 id(例如:choice[n] 和 checkChoice[n] 对):

    <input type="text" name="choice[13]" id="choice" placeholder="Enter your Choice" class="form-control" /> </td>  
    <input class="form-check-input" type="radio" name="checkChoice[13]" id="checkChoice" value="1">
    

    之后,使用 foreach 并验证是否发送了与循环中的选择具有相同索引的 checkChoice:

    foreach($data_answer as $index => $value) {
      array_push($value, array(
            'answer' => $value,
            'question_id' => $this->db->insert_id(),
            'correct_id' => ((isset($_POST['checkChoice'][$index])) ? 1 : null)
        ));
    }
    

    因为现在每个收音机都有不同的名称,所以收音机就像一个复选框(允许您选择多个)。因此,您必须使用 jquery sn-p 再次强制执行无线电行为:

    $( document ).ready(function() {
      $('input[type=radio]').change(function() {
         // When any radio button on the page is selected,
         // then deselect all other radio buttons.
         $('input[type=radio]:checked').not(this).prop('checked', false);
     });
    });
    

    【讨论】:

    • 你好,为什么我还要在我的输入中输入[13]?如果有不同的数字怎么办?
    • 其实你可以使用任何数字(13用来说明配对情况)。它可以是顺序的,例如choice[1]、choice[2]、choice[3]、choice[n]、...,并且与其相关的对 checkChoice[1]、checkChoice[2]、checkChoice[3 ],检查选择[n]。在两个相关字段中使用相同的索引,可以在服务器中链接对。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多