jQuery populate items into a Select using jQuery ajax json, php

后端 未结 4 450
感动是毒
感动是毒 2020-12-14 05:05

I have a select field. I must fill with options taken from a mysql table.
Here is some little php code I have done using codeigniter framework

$idcateg =         


        
4条回答
  •  一生所求
    2020-12-14 05:28

    Try the following Code.

    In Controller ---------

    public function AjaxTest() {
    
                $rollNumber = $this->input->post('rollNumber');
                $query = "";
    
                if($rollNumber !="")
                {
                   $query = $this->welcome_model->get_students();
                }
                else
                {
                   $query = $this->welcome_model->get_students_informationByRoll($rollNumber);
                }
    
                $array = array($query);
                header('Content-Type: application/json', true);
                echo json_encode($array);
    
            }
    

    In View Add a Select Option

    
    
    
         
    

    Now Scripts ----

    function CheckAjaxCall()
                {
                    $.ajax({
                        type:'POST',
                        url:'welcome/AjaxTest',                    
                        dataType:'json',
                        data:{rollNumber: $('#txtSearchRoll').val()},                    
                        cache:false,
                        success:function(aData){ 
    
                            $('#myStudents').get(0).options.length = 0;
                            $('#myStudents').get(0).options[0] = new Option("--Select--", "0");        
    
                            $.each(aData, function(i,item) {
                            $('#myStudents').get(0).options[$('#myStudents').get(0).options.length] = new Option(item[i].Name, item[i].roll);
                                                                                                                // Display      Value
                        });
    
                        },
                        error:function(){alert("Connection Is Not Available");}
                    });
    
                    return false;
                }
    

    Enjoy the code....

提交回复
热议问题