0

I try to display data on the bootstrap select option with ajax, but after I run the data do not want to appear what is wrong?

function get_value() {
    $id = 5;
    $q = $this->db->select('*')
                      ->from('tbl_v')
                      ->where('id', $id)
                      ->order_by('id', 'ASC')
                      ->get();

    $result = $q->result();
    echo json_encode($result );


}   

$(".change").change(function(){
    var value=$(this).val();
    if(value>0){
        $.ajax({
            url: base_url+"get_value",  
            type:"POST",
            dataType: 'json',
            cache: false,               
            data:{id:value},
            success: function(respond){
                $(".get-change").html(respond).selectpicker('refresh');
            }
        })
    }
});
irwan dwiyanto
  • 690
  • 1
  • 9
  • 28

2 Answers2

0

Try this...

function OnChangeValue()
{
    var pdrp = document.getElementById("yourControlIDHere");
    getvalue= pdrp.options[pdrp.selectedIndex].value;
    alert(getvalue);
}

The Control

<select id="yourControlIDHere" onchange="OnChangeValue(this)" name="companyname" style="width:100%">
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
0

Jquery Code

$(".change").change(function(){
var value=$(this).val();
if(value>0){
    $.ajax({
        url: base_url+"get_value",  
        type:"POST",
        cache: false,               
        data:{id:value},
        success: function(respond){
            $(".get-change").html(respond);
        }
    })
}
});

Modal Code:

function get_records() {
    $id = 5;
    $q = $this->db->select('*')
                      ->from('tbl_v')
                      ->where('id', $id)
                      ->order_by('id', 'ASC')
                      ->get();

    $result = $q->result();
}   

Controller Code:

function get_value(){
   $ido = $this->input->post('id');
   $data = $this->your-model-name->get_records($ido);
   $option = "";
   foreach($data as $row){
      $option .= "<option value='".$value['ID']."'>".$row[0]->name."</option>";
   }
   echo $option;
}

You need not have to use JSON encoding. Records will be directly appended to the select box. Hope this can help you.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28
  • thank you for the answer, actually already appear but there are constraints when I use bootstrap plugin selecpicker list does not appear, – irwan dwiyanto Dec 18 '17 at 09:35
  • Can you give me more details? What constraints are you talking about? – BEingprabhU Dec 18 '17 at 09:38
  • Do you want to put any conditions in the query? – BEingprabhU Dec 18 '17 at 09:39
  • so I made a dynamic select option with bootstrap, previously it was already run using `class="form-control"` but when I use jquery plugins bootstrap-select `class="selecpicker"` data does not show. – irwan dwiyanto Dec 18 '17 at 09:41
  • What problem do you have in using a select box with `form-control`? Is there any special functionality in it? I was just googling about your problem. I found it is just a class difference. Both are bootstrap select boxes. Can you tell me for what purpose you are using?? – BEingprabhU Dec 18 '17 at 09:52