Skip to content

Instantly share code, notes, and snippets.

@RakibSiddiquee
Last active May 3, 2017 11:50
Show Gist options
  • Save RakibSiddiquee/90a0a4069dbce35cad5e45fe00447755 to your computer and use it in GitHub Desktop.
Save RakibSiddiquee/90a0a4069dbce35cad5e45fe00447755 to your computer and use it in GitHub Desktop.
Populate one dropdown when select another dropdown. You must include the jquery file top of your code...
// There are two ways...
// No:1.......
$(function() {
$('#category').change(function() {
var cat = $(this).val();
$.get('{{ url("categories") }}', {'cat_id': cat}, function (data) {
$('#subcategory').empty();
$.each(data, function(key, value) {
$('#subcategory').append("<option value='"+ value.id +"'>" + value.name + "</option>");
});
});
});
});
// No:2............
$('#upozilla').prepend('<option value="1" selected>--None--</option>');
$('#district').on('change', function(e){ // Pre-populate upozilla with selecting district dropdown
console.log(e);
var district_id = e.target.value;
$('#upozilla').empty();
$.get('{{ url("/upozilla-populate?district_id=") }}'+district_id, function(data){
$.each(data, function(index, upozillaObj){
$("#upozilla").empty();
$('#upozilla').append('<option value="'+upozillaObj.id+'">'+upozillaObj.upozilla_name_bn+'</option>');
});
$('#upozilla').prepend('<option value="1" selected>--None--</option>');
});
});
// Then get the cat_id or district_id in php file and select data and return the data in json format
// In laravel get the id in your controller and return the data in json format
@MosharofHossen
Copy link

Yes sir, this is a cascading select functionality in html format. but, I will need your help to apply this in laravel. I read the last line but yet this too complex to me. .... ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment