我已经创建了一个在线考试系统,我添加了问题和不同的选项,如下面的截图:
我的问题:我在上面的截图中添加了问题(如1 + 1 =?)及其不同的选项,所以我检查了正确的答案和数据库插入值,如1
查看:
<?php echo form_open('admin/Examinations/add_questions/' . $examination_test_id, $attributes); ?>
<div class="box-body">
<input type="hidden" name="examination_test_id" value="<?php echo $result['examination_test_id']; ?>"/>
<div class="form-group">
<label>Question Name</label>
<textarea name="question_name" id="question_name" class="form-control" rows="2" placeholder="Enter Question"></textarea><br/>
<input id="btnAdd" type="button" value="Add Options" class="btn btn-primary"/>
</div>
<div id="TextBoxContainer" class="form-group">
<!-- Textboxes will be added here -->
</div>
</div>
<div class="box-footer">
<button type="submit" name="submit" id="submit" class="btn btn-primary">Submit</button>
</div>
<?php echo form_close(); ?>
<script>
$(function () {
$("#btnAdd").bind("click", function () {
var div = $("<div />");
div.html(GetDynamicTextBox(""));
$("#TextBoxContainer").append(div);
});
$("body").on("click", ".btn-danger", function () {
$(this).closest("div").remove();
});
});
function GetDynamicTextBox(value) {
return '<table class="table table-bordered">' +
'<tr>' +
'<td style="width:140%">' + '<input name = "DynamicTextBox[]" type="text" value = "' + value + '" class="form-control" placeholder="Enter Options"/> ' + '</td>' +
'<td>' + '<input type="radio" name="gender" id="gender" value="1" class="flat-red"/>' + '</td>' +
'<td>' + '<input type="button" value="Remove" class="btn btn-danger"/>' + '</td>' +
'</tr>' +
'</table>'
}
</script>
型号:
public function insert($last_id, $data) {
var_dump($data);
for ($i = 0; $i < count($data['DynamicTextBox']); $i++) {
$insert_data = array(
'question_id' => $last_id,
'option_name' => $data['DynamicTextBox'][$i],
//'corrrect_answer' => $data['gender'][$i]
);
if ($data['gender'] == 1) {
$insert_data['corrrect_answer'] = $data['gender'][$i];
} else {
$insert_data['correct_answer'] = [$i];
}
$this->db->insert('examination_option', $insert_data);
}
}
控制器
class Examinations extends CI_Controller {
public function add_questions($examination_test_id) {
var_dump($_POST);
$this->form_validation->set_rules('question_name', 'Question Name', 'required');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Add Questions',
'page_name' => 'Examinations/add_questions',
'admin_username' => $this->session->userdata('username'),
'result' => $this->examination_test_model->select_id($examination_test_id)
);
$data['examination_test_id'] = $examination_test_id;
$this->load->view('admin/template', $data);
} else {
$this->examination_question_model->insert($_POST);
// redirect('admin/Examinations');
}
}
}
var_dump结果
array (size=5)
'examination_test_id' => string '1' (length=1)
'question_name' => string '1+1= ?' (length=6)
'DynamicTextBox' =>
array (size=5)
0 => string '3' (length=1)
1 => string '2' (length=1)
2 => string '4' (length=1)
3 => string '5' (length=1)
4 => string '6' (length=1)
'gender' => string '1' (length=1)
'submit' => string '' (length=0)
我的问题:
我在上面的截图中添加了问题(如1 + 1 =?)及其不同的选项,所以我检查了正确的答案和dataabse插入值如1
答案 0 :(得分:0)
提交表单提交表单元素名称和相应的值, 正如我在这种情况下可以看到的那样,有一个单选按钮,其默认值为&#34; 1&#34;
'<td>' + '<input type="radio" name="gender" id="gender" value="1" class="flat-red"/>' + '</td>'
您正在生成包含文本框和单选按钮的整行,但单选按钮的值在所有行中都相同。
您的不同单选按钮需要具有不同的值,以便您可以保存所有答案以及设置为&#34的答案;正确答案&#34;。