![]() |
Choosing a chained select option with jquery using Codeigniter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Choosing CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=8) +--- Thread: Choosing a chained select option with jquery using Codeigniter (/showthread.php?tid=83445) |
Choosing a chained select option with jquery using Codeigniter - JernalinSadhoo - 09-27-2022 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. |