I am trying to make the associated combo boxes with values ​​from the database, but there was a difficulty. html field code:

<?php require('scripts/connect.php'); $query = mysqli_query($link,"select name from category where level=1"); $rows = mysqli_num_rows($query); echo "<select name='id_category' id='id_category'>"; echo "<option value='0'>- выберите категорию -</option>"; for($i=0; $i<$rows; $i++) { $mas = mysqli_fetch_array($query); echo "<option value='".($i+1)."'>".$mas["name"]."</option>"; } echo "</select>"; ?> <select name="id_category_2" id="id_category_2" disabled="disabled" class="StyleSelectBox"> <option value="0">- выберите категорию -</option> </select> 

JQuery code for selector select functions:

 $(document).ready(function () { $('#id_category').change(function () { var id_category = $(this).val(); if (id_category == '0') { $('#id_category_2').html('<option>- выберите регион -</option>'); $('#id_category_2').attr('disabled', true); return(false); } $('#id_category_2').attr('disabled', true); $('#id_category_2').html('<option>загрузка...</option>'); var url = "get_category.php"; $.get( url, "id_category=" + id_category, function (result) { if (result.type == 'error') { alert('error'); return(false); } else { var options = ''; $(result.id_category_2).each(function() { options += '<option value="' + $(this).attr('id_category_2') + '">' + $(this).attr('name') + '</option>'; }); $('#id_category_2').html('<option value="0">- выберите категорию -</option>'+options); $('#id_category_2').attr('disabled', false); } }, "json" ); }); }); 

The php file code for selecting categories:

 <?php include_once('connect.php'); $id_category = @intval($_GET['id_category']); echo $id_category; $cats=mysqli_query($link, "SELECT id_category, name, pid FROM category WHERE pid=($id_category)"); if ($cats) { $num = mysqli_num_rows($cats); $i = 0; while ($i < $num) { $id_category_2[$i] = mysqli_fetch_assoc($cats); $i++; } $result = array('id_category_2'=>$id_category_2); } else { $result = array('type'=>'error'); } print json_encode($result); ?> 

When selecting a category, the field remains inactive, but even if you turn off the switch off of the select, then the "download" hangs and the list is not populated: enter image description here

    1 answer 1

    Try to enable / disable your second select via .attr('disabled', false); , and through .prop('disabled', false);

    Or remove the attribute altogether .removeAttr('disabled');

    • This is not the case, even if you turn on select, the list is not filled, and the loading is hanging - unit
    • So you do not work out the else {} block in the $ .get results. Inside $ (result.id_category_2) .each (function () {output the results. - mJeevas
    • There are no results, options variable is not populated, as far as I understand. - unit