In this image I want to select category then I want subcategory in another drop down. On selecting value in Category I want to call Ajax which fetch subcatcategory from database and put it into another subcategory drop down. How to do this?
Asked
Active
Viewed 561 times
0
-
Possible duplicate of [retrieve dropdown list from mysql database and insert to database in jsp](http://stackoverflow.com/questions/6168836/retrieve-dropdown-list-from-mysql-database-and-insert-to-database-in-jsp) – Lakmal Vithanage Apr 24 '16 at 09:58
-
Title is still undecipherable, but maybe other will have the time to fix it. – peterh Apr 26 '16 at 10:09
2 Answers
0
Since this is the client side, you need to get the data from the database through the server. You can read this link for more details.

Community
- 1
- 1

Lakmal Vithanage
- 2,767
- 7
- 42
- 58
0
In your view page call a ajax function where are want to change sub category on chage category
$(document).on('change', '#cat_id', function () {
var cat_id = $(this).val();
$.ajax({
type: 'POST',
url: '<?php echo site_url('controller_name/subCatByCatId'); ?>', // here call your function where you want to send cat_id
data: {cat_id: cat_id},
success: function (data) {
$('#sub_cat_id').html(data);
}
});
});
And here is your server side function
function subCatByCatId() {
$cat_id = $_POST['cat_id'];
$query = $this->db->query("select * from sub_category where cat_id=$cat_id")->result();//here replace your query
$returnVal = '<option value = "">Select one</option>';
if (!empty($query)) {
foreach ($query as $row) {
$returnVal .= '<option value = "' . $row->sub_cat_id . '">' . $row->sub_cat_name . '</option>';
}
}
echo $returnVal;
}

Rakib Roni
- 252
- 3
- 12
-
-
well its not matter if you can understand it hope you can implement in your project – Rakib Roni Apr 24 '16 at 10:24
-
-