Dropdown Dependent in Laravel php with jQuery AJAX

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024
  • How to Create a Dropdown Dependent in Laravel 8 with jQuery AJAX
    country city state dropdown
    code in comment

Комментарии • 1

  • @online_programming
    @online_programming  Год назад

    Laravel Dropdown



    Laravel Dependent Dropdown



    countries

    Choose countries
    @foreach ($countries as $item)
    {{ $item->name }}
    @endforeach



    cities



    states








    $(document).ready(function() {
    $('#countries').on('change', function() {
    var country_id = $(this).val();
    if(country_id) {
    $.ajax({
    url: 'localhost/dropdown/public/getCities/'+country_id,
    type: "GET",
    dataType: "json",
    success:function(data)
    {
    if(data){
    $('#cities').empty();
    $('#cities').append('Choose cities');
    $.each(data, function(key, cities){
    $('select[name="cities"]').append('' + cities.name+ '');
    });
    }else{
    $('#cities').empty();
    }
    }
    });
    }else{
    $('#cities').empty();
    }
    });
    $('#cities').on('change', function() {
    var city_id = $(this).val();
    if(city_id) {
    $.ajax({
    url: 'localhost/dropdown/public/getStates/'+city_id,
    type: "GET",
    dataType: "json",
    success:function(data)
    {
    if(data){
    $('#states').empty();
    $('#states').append('Choose states');
    $.each(data, function(key, states){
    $('select[name="states"]').append('' + states.name+ '');
    });
    }else{
    $('#states').empty();
    }
    }
    });
    }else{
    $('#states').empty();
    }
    });
    });