P
P
proger_102017-11-19 20:48:59
PHP
proger_10, 2017-11-19 20:48:59

2 linked dropdowns + mysql - PHP?

Guys, hello everyone!
Please help me figure it out: I have 2 selects on the page, I successfully filled one of them from the database.
But I cannot make the second select in any way so that it is filled depending on the selected first select.
Let me explain with an example: in the first drop-down list, select a profession, for example, "engineer", in the second, you need to remove from the database those people who have this profession.
en_main.php

// в head прописал <script src="../main/select.js"></script>
<select id="position_list" ><?php position_list();?>
<select id="spec_list" disabled="disabled"><option value="0">Выберите из списка</option></select>

selects.js
$(document).ready(function () {
    $('#position_list').change(function () {
        var position_list = $(this).val();
        if (position_list == '0') {
            $('#spec_list').html('<option>Выберите из списка</option>');
            $('#spec_list').attr('disabled', true);
            return(false);
        }
        $('#spec_list').attr('disabled', true);
        $('#spec_list').html('<option>загрузка...</option>');
        
        var url = '../main/get_spec.php';

        $.get(
            url,
            "position_list=" + position_list,

            function (result) {
                if (result.type == 'error') {
                    alert('error');
                    return(false);
                }
                else {
                    var options = '';
                    $(result.fio).each(function() { 
                        options += '<option value="' + $(this).attr('spec_list') + '">' + $(this).attr('name_per') + '</option>';
                    });
 
                    $('#spec_list').html('<option value="0">Выберите из списка</option>'+options);
                    $('#spec_list').attr('disabled', false);         
                }
            },
            "json"
        );
    });
});

get_spec.php
<?php
  ini_set('error_reporting', E_ALL);
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  include_once 'connect.php';
  mysqli_set_charset($connect, 'utf8');
  $position_list = @intval($_GET['position_list']);
  //$position_list = 6;
  //echo $position_list;
  $fio_spec="SELECT name_per FROM personal WHERE id_per=$position_list";
  $pers = mysqli_query($connect, $fio_spec) or die(mysqli_error($connect)); 
  if ($pers) {
      $num = mysqli_num_rows($pers);      
      $i = 0;
      while ($i < $num) {
         $fio[$i] = mysqli_fetch_assoc($pers);   
         $i++;
      }     
      //var_dump($fio);
      $result = array('fio'=>$fio);  
//print_r($fio);
  }
  else {
  	  $result  = array('type'=>'error');
  }
  print json_encode($result);
?>

At the moment, the second select is loading "loading ...". Checked that get_spec.php gives correct information in fio. Therefore, most likely an error in accepting data in selects, js, and therefore the drop-down list is not formed.
Please help me figure it out

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question