选择选项无法使用编码器在数据库中提交



屏幕截图

型号lesson_model.php

function fetch_data() {   
$course_code = $this->session->userdata('course_code'); 
$this->db->select('*');  
$this->db->from('chapter');
$this->db->order_by('chapter.chapter_number', 'ASC');
$this->db->where('course_code', $course_code);
$query = $this->db->get();  
return $query;  
}

控制器Add_Lesson.php

function __construct() { 
parent::__construct(); 
$this->load->helper('url');
$this->load->model('lesson_model');
$this->load->database(); 
} 
public function index() {
$data["fetch_data"] = $this->lesson_model->fetch_data();
$this->load->view("lesson/add", $data);
}

查看添加.php

<div class="row">
<div class="col-md-2 form-group">
<label class="col-sm-12">Chapter Number</label>
<div class="col-sm-9">
<select type="text" id="chapter_number" name="chapter_number" class="form-control form-control-line">
<option disabled="" selected="">Select</option>
<?php foreach($option->result() as $row) {        
echo '<option value="'.$row->chapter_title.'">'.$row->chapter_number.'</option>';   
} 
?> 
</select>
<span class="text-danger"><?php echo form_error("chapter_number"); ?></span>
</div>
</div>
<div class="col-md-9 form-group">  
<label class="col-md-12">Chapter Title</label>
<div class="col-md-10">
<input type="text" id="title" name="chapter_title" class="form-control"/>
<span class="text-danger"><?php echo form_error("chapter_title"); ?></span>
</div>
</div>
<script>
$('#chapter_number').on('change', function() {
var selected = $( "#chapter_number" ).val();
$('input[name="chapter_title"]').val(selected);
});
</script>
</div

唯一的问题是,当我提交时,即使我将章节编号设置为 1,只有章节标题会正确出现在数据库中,并且章节编号为 0。

我认为您在第一次选择中使用了错误的值。 您应该使用章节编号而不是标题,因为数字是一个整数,它不会像对待章节标题那样采用字符串变量。更新您的代码

<div class="col-md-2 form-group">
<label class="col-sm-12">Chapter Number</label>
<div class="col-sm-9">
<select type="text" id="chapter_number" name="chapter_number" class="form-control form-control-line">
<option disabled="" selected="">Select</option>
<?php foreach($option->result() as $row) {        
echo '<option value="'.$row->chapter_number.'">'.$row->chapter_number.'</option>';   
} 
?> 
</select>
<span class="text-danger"><?php echo form_error("chapter_number"); ?></span>
</div>

相关内容

  • 没有找到相关文章

最新更新