Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

codeigniter - 使用codeigniter从下拉列表填充输入字段(Populate input field from a dropdown using codeigniter)

chapter_number | chapter_title |
       1       | Introduction  |
       2       | Number System |


<select type="text" name="chapter_number" class="form-control form-control-line" value="">
    <option selected="option" disabled="selected">Select</option>   
    <option value="1">1</option>
    <option value="2">2</option>
</select

<input type="text" name="chapter_title" id="title" class="form-control"/>

above is my table and next is my code the select option and the input field.

(上面是我的表格,接下来是我的代码select选项和输入字段。)

I want that when I select '1' automatically the input field will print chapter title 'Introduction' and If I select '2' the input field will became 'Number System'.

(我希望当我自动选择“ 1”时,输入字段将打印章节标题“ Introduction”,如果我选择“ 2”,则输入字段将变为“ Number System”。)

  ask by Christian Jamila translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to get data from database you need to call ajax on onchange event of select box.

(如果要从数据库获取数据,则需要在选择框的onchange事件上调用ajax。)

Add "setinput" class to select box

(将“ setinput”类添加到选择框)

<select type="text" name="chapter_number" class="form-control form-control-line setinput" value="">

Add below script

(添加以下脚本)

$('body').on('change','.setinput',function(){
    $.ajax({
        url: 'your_base_url/controller_name/your_method',
        type:'POST',
        data:{id:$(this).val()},
        success:function(result){
            $('input[name="chapter_title"]').val(result);
        }
    })

});

Controller method would be like below

(控制器方法如下)

public function your_method(){
    $id = $this->input->post('id');
    $this->db->select('*');
    $this->db->from('database_table_name');
    $this->db->where('chapter_number',$id);
    $result = $this->db->get()->row();
    echo $result->chapter_title;
}

I hope, it would be helpful to you.

(希望对您有帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...