Choosing a chained select option with jquery using Codeigniter |
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 contains: category_id, category_name, brand_id. |
Messages In This Thread |
Choosing a chained select option with jquery using Codeigniter - by JernalinSadhoo - 09-27-2022, 04:05 AM
|