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;
?>