我想根据所选品牌过滤产品,我正在获取json格式的产品。但是它没有显示在表单中,我该如何回显产品名称

时间:2018-08-31 06:33:52

标签: php jquery json codeigniter

我的表单(视图)

<?php echo form_open(get_uri("leads/save_lead_items"), array("id" => "lead_items-form", "class" => "general-form", "role" => "form")); ?>
<div class="modal-body clearfix">
    <fieldset>

            <?php
                    echo form_input(array(
                        "id" => "leads_id",
                        "name" => "leads_id",
                        "class" => "form-control",
                        "type" => "hidden",
                        "readonly" => true,
                        "value" => $lead_info->id
                    ));
                ?>
        <div class="form-group">
            <?php echo form_label(lang("select_brand"), 'brand'); ?>
            <?php
                    echo form_dropdown(array(
                        "id" => "brand",
                        "name" => "brand",
                        "class" => "form-control",
                        "data-rule-required" => true,
                        "data-msg-required" => lang("field_required")),
                        $brands_dropdown, array($brand_info->id));
                ?>
        </div>

        <div class="form-group">
            <?php echo form_label(lang("select_product"), 'product'); ?>
            <?php
                    echo form_dropdown(array(
                      "id" => "product",
                      "name" => "product",
                      "class" => "form-control"));
                        // $product_dropdown, array($product_info->id));
                ?>
        </div>

        <div class="form-group">
            <?php echo form_label(lang("quantity"), 'quantity'); ?>
            <?php
                      echo form_input(array(
                          "id" => "quantity",
                          "name" => "quantity",
                          "class" => "form-control",
                          "placeholder" => 'Enter the Quantity',
                          "autofocus" => true,
                          "data-rule-required" => true,
                          "data-msg-required" => lang("field_required")
                      ));
                ?>
        </div>
    </fieldset>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">
            <span class="fa fa-close"></span>
            <?php echo lang('close'); ?>
        </button>
        <button type="submit" class="btn btn-primary"><span class="fa fa-check-circle"></span> <?php echo lang('save'); ?></button>
    </div>
</div>
<?php echo form_close(); ?>

<script type="text/javascript">
$("#brand").change(function () {
    //get the value of the brand when it changes
    var brandID = $("#brand").val()

    //make an ajax request posting it to your controller
    $.post('<?=base_url("leads/get_all_related_data_of_brand")?>', {data:brandID}, function(result) {
      $.each(result, function(key,value){
    $('#product').append('<option value="'+result.key+'">'+result.value+'</option>');
});

});
    });
</script> 

我的控制器功能

 public function get_all_related_data_of_brand() {
    if($this->input->is_ajax_request()){
        $brand_id = $this->input->post('data');
    if ($brand_id) {

        $products = $this->Products_model->get_products_dropdown_list($brand_id)->result();
        $products_dropdown = array(array("id" => "", "name" => "-"));
        foreach ($products as $product) {
            $products_dropdown[] = array("id" => $product->id, "name" => $product->name);
        }
        echo json_encode(array("result" => $products_dropdown));

    }
}
}

我的模型功能

function get_products_dropdown_list($brands_id = 0) {
    $products_table = $this->db->dbprefix('products');
    $brands_table = $this->db->dbprefix('brands');

    $where = " AND $products_table.brands_id = $brands_id";

    $sql = "SELECT $products_table.name, $products_table.id
    FROM $products_table
    LEFT JOIN $brands_table ON $products_table.brands_id=$brands_table.id
    WHERE $products_table.deleted=0 $where 
    ORDER BY $products_table.name ASC";
    return $this->db->query($sql);
}
}

我想根据用户选择的品牌过滤产品。但是产品名称未显示在表单中。 我想在表格下拉列表中显示过滤产品的名称。在json中回显后,我无法显示名称。

enter image description here

0 个答案:

没有答案