我已成功插入多个值,现在要查询 问题 ,如何同时更新来更新correct_id
到1 -如何使用单选按钮作为参考值更新所选输入。
这是我的界面,关于界面的设计方式。
我有一个字段名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>
答案 0 :(得分:0)
首先,为每个答案单选和文本字段(例如:choice [n]和checkChoice [n]对)分配不同的索引随机ID:
<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片段再次强制执行无线电行为:
$( 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);
});
});