Dynamic Combobox Information Filter

0

I would like to know how to do a dynamic data filter, so only the data related to the selected one appears, for example:
I have in my bank the table Empregado and Empresa , as an example I have:

  • Employee 1 (fk_company 1), Work at Company 1;
  • Employee 2 (fk_company 1), Work at Company 1;
  • Employee 3 (fk_company 2), Work at Company 2;
  • Officer 4 (fk_company 2), Works in Company 2;

So when I clicked on Combobox, and I selected Company 1, I wanted to see only employees 1 and 2 on the combobox below. But now I would like to know how to do it, it should probably be in AJAX, but AJAX I do not know much so I ask you for help.

Comboboxes Code:
Company:

<div class="profile-info-row">
    <div class="profile-info-name"> **Empresa** </div>  
        <div class="profile-info-value">
            <select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
            <option value="" selected disabled="disabled" hidden>Selecione a Empresa</option>
        <?php $line = $searchSQL->fetchAll(PDO::FETCH_ASSOC);
            foreach($line as $thread):?>
             <option value="<?php echo $thread['cod_empresa']; ?>"><?php echo $thread['razao_social']; ?></option> <?php endforeach; ?>
            </select>
        </div>
    </div>

Employee:

<div class="profile-info-row">
    <div class="profile-info-name"> Nome do Empregado </div>
        <div class="profile-info-value">
        <select name="empregado" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
        <option value="" selected disabled="disabled" hidden>Selecione o Empregado</option>
        <?php 
        $plataform = $requestSQL->fetchAll(PDO::FETCH_ASSOC);
        foreach($plataform as $contour): ?>
         <option value="<?php echo $contour['cod_empregado']; ?>"><?php echo $contour['nome']; ?></option> <?php endforeach; ?>
        </select>
    </div>
</div>

EDIT
According to suggestions I've refactored some parts, and despite the company ID being picked up (proven by alert), it's not returning anything, why? Follow the file for preview
FilterEmp file:

<?php
    require 'conexao.php';
    $pdo = conectar();

    if (isset($_POST['id_empresa'])) :
        try{    
        $cod_empresa = $_POST['id_empresa'];
        $SQL = "SELECT * FROM tbl_empregado WHERE fk_empresa = ?";  
        $stmt = $pdo->prepare( $SQL );
        $stmt->bindValue(1, $cod_empresa, PDO::PARAM_INT);
        $stmt->execute();
        $row=$stmt->fetch(PDO::FETCH_ASSOC);
        extract($row);

    }catch(PDOException $e){
        'ERROR :' . $e->getMessage()."<br>";
        'ERROR :' . $e->getCode();
    }endif;
?>
    
asked by anonymous 28.03.2017 / 15:12

1 answer

0

You should use the same ajax.

Using jquery would look like this:

  • Create a script that returns only filtered employees per company, such as the Empregados that you put in the example.

  • Use the jquery ajax function when the value of the empresa_destino field changes: / p>

  • $("select[name=empresa_destino]").change(function() {
      var id_empresa = $(this).val();
      $.ajax({
        'url': 'empregados.php',
        'data': {'id_empresa': id_empresa},
        'method': 'POST',
        'success': function(returnHtml) {
          $(".resultado").html(returnHtml);
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="profile-info-row">
        <div class="profile-info-name"> **Empresa** </div>  
            <div class="profile-info-value">
                <select name="empresa_destino" id="form-field-1" class="col-xs-11 col-sm-6 col-md-8">
                <option value="" selected disabled="disabled" hidden>Selecione a Empresa</option>
                <option value="1"> Empresa 1</option>
                <option value="2"> Empresa 2</option>
                </select>
            </div>
        </div>
        <div class="resultado"></div>
        
    28.03.2017 / 15:32