Welcome Guest, Not a member yet? Register   Sign In
Choosing a chained select option with jquery using Codeigniter
#1

(This post was last modified: 10-10-2022, 03:47 AM by JiwanFilembar.)

I'm not much good at jquery and ajax, and I'm now having difficulties on a select box. I use CI and My code is below.

Another select box "category" data will be show according to the "brand". How can I carry data from "brand" and show data in "category" with jquery?

View

<select name="brand" class="form-control" id="brand" required>
    <?php
        if($items) {
            foreach($items as $key) {
    ?>
    <option value="<?php echo $key->brand_id ?>">
        <?php echo $key->brand_name ?>
    </option>
    <?php
            }
        }
    ?>
</select>

<select name="category" class="form-control" id="category" required>

</select>
Ajax

<script>
    $(function() {
        $("#brand").on('change', function() {
            var brand = $(this).val();
            $.ajax ({
                type: "post",
                url: "<?php echo base_url(); ?>receiving/showCategory",
                dataType: 'json',
                data: 'brand='+brand,

                success: function(msg) {
                    var options;
                    for(var i = 0; i<msg.length; i++) {
                        options = '<option>'+msg.category[i].category_name+'</option'>;
                    }
                    $('#category').html(options);
                }
            });
        });
    });
</script>
Controller

function showCategory() {
    $brand_id = $this->input->post('brand');
    $data['category'] = $this->item_model->category($brand_id);
    echo json_encode($data);
    }
My category table omegle shagle voojio contains: category_id, category_name, brand_id.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB