使用ajax php codeigniter填充下拉菜单

时间:2022-10-07 00:28:20

I have been trying all the help available on different forums but now give up and posting my question here. Trying to populate dropdown using ajax call. Getting the data in json format successfully. But don't know to fill dropdown. code below: Controller:

我已经在不同的论坛上尝试了所有的帮助,但是现在放弃,把我的问题贴在这里。尝试使用ajax调用填充下拉列表。成功地获取json格式的数据。但不知道填充下拉列表。下列代码:控制器:

function getRegion()
{
    $this->load->model('Settings_model');
    $title = $this->input->get('title');

    $result =   array("region" => $this->Settings_model->getSelectedRegion($title));
    echo json_encode($result);      
}

View:

观点:

        $(document).on('change', '#campaignSel', function() {
        changecampaign();
    });

    function changecampaign(){
        var title= "New Normal";//$('#campaignSel option:selected').text();//$('#campaignSel').text();
        $regionSel = $("#regionSel");
            $.ajax({
                type:"GET",
                url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
                data:{ "title":"New Normal"},
                datatype: "json",
                success: function(result){
                    var appenddata;
                    $.each(result, function (key, value) {
                        appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";                        
                    });
                    $('#regionSel').html(appenddata);
                },
                error: function(xhr, textStatus, error){
                    console.log(xhr.statusText);
                    console.log(textStatus);
                    console.log(error);
                }
            });
        }

Response: {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY ELEC"}]} I want to fill dropdown with Regions.

响应:{“区域”:【{“id”:“1”,“区域”:“109翼”,“I”,“id”:“2”,“区域”:“ASHBURTON/MID CANTERBURY”},我想要填写下拉列表。

2 个解决方案

#1


1  

you need to loop the region like this $.each(result.region, function (key, value) { instead of result

您需要循环该区域,如$.each(结果)。区域,函数(键,值){而不是结果

Update1:

Update1:

added if condition if ($.isArray(result.region)){ ... }

添加if条件if ($.isArray(result.region))}

result =  {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY ELEC"}]}
var appenddata='';
if ($.isArray(result.region)){
$.each(result.region, function (key, value) {
                        appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";    
                        
                        console.log(appenddata);
                    });
                    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#2


0  

Finally I resolved the issues in my code. I like to share the changes I made to my code below: added:

最后我解决了代码中的问题。我想分享我对我的代码所做的修改:

contentType: "application/json",

and

parsedobj = JSON.parse(result)

so the final ajax code is:

最后的ajax代码是:

$.ajax({
                type:"GET",
                url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
                data:{ "title":title},
                contentType: "application/json",
                datatype: "json",
                success: function(result){
                     parsedobj = JSON.parse(result)
                     var appenddata='';
                        $.each(parsedobj.region, function(index, value) 
                        {
                            appenddata += "<option value = '" + index + "'>" + value.region + " </option>";    
                        });

                        $('#regionSel').html(appenddata); 
                },
                error: function(xhr, textStatus, error){
                    console.log(xhr.statusText);
                    console.log(textStatus);
                    console.log(error);
                }
            });

#1


1  

you need to loop the region like this $.each(result.region, function (key, value) { instead of result

您需要循环该区域,如$.each(结果)。区域,函数(键,值){而不是结果

Update1:

Update1:

added if condition if ($.isArray(result.region)){ ... }

添加if条件if ($.isArray(result.region))}

result =  {"region":[{"id":"1","region":"109 FEATHERSTON ST (ELEC)\r\n"},{"id":"2","region":"ASHBURTON/MID CANTERBURY ELEC"}]}
var appenddata='';
if ($.isArray(result.region)){
$.each(result.region, function (key, value) {
                        appenddata += "<option value = '" + value.id + " '>" + value.region + " </option>";    
                        
                        console.log(appenddata);
                    });
                    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#2


0  

Finally I resolved the issues in my code. I like to share the changes I made to my code below: added:

最后我解决了代码中的问题。我想分享我对我的代码所做的修改:

contentType: "application/json",

and

parsedobj = JSON.parse(result)

so the final ajax code is:

最后的ajax代码是:

$.ajax({
                type:"GET",
                url: "<?php echo base_url('Pricecomparison/getRegion'); ?>",
                data:{ "title":title},
                contentType: "application/json",
                datatype: "json",
                success: function(result){
                     parsedobj = JSON.parse(result)
                     var appenddata='';
                        $.each(parsedobj.region, function(index, value) 
                        {
                            appenddata += "<option value = '" + index + "'>" + value.region + " </option>";    
                        });

                        $('#regionSel').html(appenddata); 
                },
                error: function(xhr, textStatus, error){
                    console.log(xhr.statusText);
                    console.log(textStatus);
                    console.log(error);
                }
            });