Dynamic Combobox Information Filter

Asked

Viewed 619 times

0

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

  • Staff 1(fk_company 1), Works at Company 1;
  • Employee 2(fk_company 1), Works at Company 1;
  • Employee 3(fk_company 2), Works at Company 2;
  • Employee 4(fk_company 2), Works at Company 2;

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

Code of Comboboxes:
Enterprise:

<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 remade some parts, and although the company ID is caught (proven by Alert), it is not returning anything, why? Follow the file for viewing
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;
?>

1 answer

0


You should use ajax even.

Using jquery would look like this:

  1. Create a script that returns only employees filtered by company, as in Empregados that you put in the example.

  2. Use the function ajax jquery, when the field value empresa_destino is amended:

$("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>
<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>
            <option value="1"> Empresa 1</option>
            <option value="2"> Empresa 2</option>
            </select>
        </div>
    </div>
    <div class="resultado"></div>

  • Hello @Vanildo I made the changes but when there is the change nothing occurs, exactly as it would be my file in PHP for query

  • I updated the post

  • 1

    I forgot to add the method in the ajax request. See how it is now, and if it works

  • Still nothing has happened

  • 1

    In the query file you should return the entire HTML, with the Divs and select already ready.

  • 1

    I did this post a while ago on my blog, more or less what you want to do: http://blog.toneladas.com.br/respondendo-no-stackoverflow-1.html

  • Okay now I understand I’m going to try to develop something here and then I show you an answer

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.