0
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. 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 Answer 1

1

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

Add "setinput" class to select box

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi sir.. <---A PHP Error was encountered Severity: Notice Message: Trying to get property 'chapter_title' of non-object---> that is the error..
the error is in the <--- echo $result->chapter_title; --->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.