var select2CategorieMadri = $("#select2CategorieMadri"); var select2Sottocategorie = $("#select2Sottocategorie"); select2CategorieMadri.select2(); select2Sottocategorie.select2(); select2CategorieMadri.on("change", function (e) { let id_categoria_madre = $('#select2CategorieMadri').val(); setSottocategorieSelect2(id_categoria_madre); }); function setSottocategorieSelect2(id_categoria_madre = ''){ $.ajax({ type: 'POST', url: 'https://www.ilcercartigianodiqualita.it/assets/ajax/ottieni-sottocategorie.php', data: { id_categoria_madre: id_categoria_madre }, dataType: 'json' }).then(function (response) { var select2Sottocategorie = $("#select2Sottocategorie"); select2Sottocategorie.empty(); var option = new Option('Seleziona una sottocategoria', '', true, false); select2Sottocategorie.append(option); response.data.forEach(function (sottocategoria) { var option = new Option(sottocategoria.etichetta_it, sottocategoria.id_categoria, false, sottocategoria.selected); select2Sottocategorie.append(option); }); select2Sottocategorie.trigger('change'); }); } $( document ).ready(function() { let id_categoria_madre = $('#select2CategorieMadri').val(); setSottocategorieSelect2(id_categoria_madre); }); select2Sottocategorie.on("change", function (e) { let id_sottocategoria = $('#select2Sottocategorie').val(); $.ajax({ type: 'POST', url: 'https://www.ilcercartigianodiqualita.it/assets/ajax/salva-opzioni-di-ricerca.php', data: { id_sottocategoria: id_sottocategoria }, dataType: 'json' }); if( document.getElementById("grado_categoria") && document.getElementById("pid_categoria")){ ottieniCategorieArtigianiPerLocalita(cod_regione, cod_provincia); } });