hello friends I am currently trying to make a select dependent, but I can not get it to show me the other select, if I inspect the item it shows me the selected value. My tables are the following:
table: employees
id_employees, names, surnames, phone, email, address
table: bank accounts
bank_account_id, employee_id, account_number, account_id, bank_id
table: accounts
account_id, accounts
table: banks
bank_id, banks
index.php
<?php
include 'funciones.php';
?>
'<div class="input-field col s12 m4">
<select name="id_empleados" id="id_empleados" required>
<option value="" disabled selected>Seleccione un Empleado:</option>
<?php
$empleados = muestro_empleados();
foreach($empleados as $indice => $registro){
echo "<option value=".$registro['id_empleados'].">".$registro['cedula']." ".$registro['nombres']." ".$registro['apellidos']."</option>";
}
?>
</select>
</div>
<div class="input-field col s12 m4">
<select name="id_bancos" id="id_bancos">
<option value="" disabled selected>Primero seleccion un empleado</option>
</select>
</div>
<div class="input-field col s12 m4">
<select name="id_cuentas" id="id_cuentas"/>
<option value="" disabled selected>Primero seleccion un banco</option>
</select>
</div>
<script>
$("#id_empleados").on("change", buscar_bancos);
$("#id_bancos").on("change", buscar_cuentas);
function buscar_bancos(){
$("#id_cuentas").html("<option value=''>- primero seleccione un id_bancos -</option>");
$id_empleados = $("#id_empleados").val();
if($id_empleados == ""){
$("#id_bancos").html("<option value=''>- primero seleccione un id_empleados -</option>");
}
else {
$.ajax({
dataType: "json",
data: {"id_empleados": $id_empleados},
url: 'funcion2.php',
type: 'post',
beforeSend: function(){
//Lo que se hace antes de enviar el formulario
},
success: function(respuesta){
//lo que se si el destino devuelve algo
$("#id_bancos").html(respuesta.html);
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\n \n responseText: "+xhr.responseText);
}
});
}
}
function buscar_cuentas(){
$id_bancos = $("#id_bancos").val();
$.ajax({
dataType: "json",
data: {"id_bancos": $id_bancos},
url: 'funcion2.php',
type: 'post',
beforeSend: function(){
//Lo que se hace antes de enviar el formulario
},
success: function(respuesta){
//lo que se si el destino devuelve algo
$("#id_cuentas").html(respuesta.html);
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\n \n responseText: "+xhr.responseText);
}
});
}
</script>'
funcional.php
<?php
function conectaBaseDatos(){
try{
$servidor = "localhost";
$puerto = "3306";
$basedatos = "sav1";
$usuario = "root";
$contrasena = "";
$conexion = new
PDO("mysql:host=$servidor;port=$puerto;dbname=$basedatos",
$usuario,
$contrasena,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$conexion->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $conexion;
}
catch (PDOException $e){
die ("No se puede conectar a la base de datos". $e->getMessage());
}
}
function muestro_empleados(){
$resultado = false;
$consulta = "SELECT * FROM empleados ORDER BY id_empleados";
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
try {
if(!$sentencia->execute()){
print_r($sentencia->errorInfo());
}
$resultado = $sentencia->fetchAll();
//$resultado = $sentencia->fetchAll(PDO::FETCH_ASSOC);
$sentencia->closeCursor();
}
catch(PDOException $e){
echo "Error al ejecutar la sentencia: \n";
print_r($e->getMessage());
}
return $resultado;
}
function muestro_bancos($id_empleados = ''){
$resultado = false;
$consulta = "SELECT
cuentas_bancarias.id_cuentas_bancarias,
cuentas_bancarias.id_empleados,
cuentas_bancarias.numero_cuenta,
cuentas_bancarias.id_cuentas,
cuentas_bancarias.id_bancos,
bancos.id_bancos,
bancos.bancos
FROM cuentas_bancarias INNER JOIN bancos ON
cuentas_bancarias.id_bancos=bancos.id_bancos";
if($id_empleados != ''){
$consulta .= " WHERE id_empleados = :id_empleados";
}
$consulta .= " ORDER BY id_cuentas_bancarias";
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
$sentencia->bindParam('id_empleados',$id_empleados);
try {
if(!$sentencia->execute()){
print_r($sentencia->errorInfo());
}
$resultado = $sentencia->fetchAll();
//$resultado = $sentencia->fetchAll(PDO::FETCH_ASSOC);
$sentencia->closeCursor();
}
catch(PDOException $e){
echo "Error al ejecutar la sentencia: \n";
print_r($e->getMessage());
}
return $resultado;
}
function muestro_cuentas($id_empleados = ''){
$resultado = false;
$consulta = "SELECT
cuentas_bancarias.id_cuentas_bancarias,
cuentas_bancarias.id_empleados,
cuentas_bancarias.numero_cuenta,
cuentas_bancarias.id_cuentas,
cuentas_bancarias.id_bancos,
cuentas.id_cuentas,
cuentas.cuentas
FROM cuentas_bancarias INNER JOIN cuentas ON
cuentas_bancarias.id_cuentas=cuentas.id_cuentas";
if($id_empleados != ''){
$consulta .= " WHERE id_empleados = :id_empleados";
}
$consulta .= " ORDER BY id_cuentas_bancarias";
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
$sentencia->bindParam('id_empleados',$id_empleados);
try {
if(!$sentencia->execute()){
print_r($sentencia->errorInfo());
}
$resultado = $sentencia->fetchAll();
//$resultado = $sentencia->fetchAll(PDO::FETCH_ASSOC);
$sentencia->closeCursor();
}
catch(PDOException $e){
echo "Error al ejecutar la sentencia: \n";
print_r($e->getMessage());
}
return $resultado;
}
?>
funcion2.php
<?php
require_once("funciones.php");
if(isset($_POST['id_empleados'])){
$cuentas_bancarias = muestro_bancos($_POST['id_empleados']);
$html = "<option value=''>- Seleccione un bancos -</option>";
foreach($cuentas_bancarias as $indice => $registro){
$html .= "<option value='".$registro['id_empleados']."'>".$registro['bancos']."</option>";
}
$respuesta = array("html"=>$html);
echo json_encode($respuesta);
}
if(isset($_POST['id_bancos'])){
$cuentas_bancarias= muestro_cuentas($_POST['id_bancos']);
$html = "<option value=''>- Seleccione una cuenta -</option>";
foreach($cuentas_bancarias as $indice => $registro){
$html .= "<option value='".$registro['id_empleados']."'>".$registro['cuentas']."</option>";
}
$respuesta = array("html"=>$html);
echo json_encode($respuesta);
}
?>
Now what I want to achieve is: in the select one the personal data of the employee is loaded, then when you select the employee you must load in the second select that banks have registered that employee, then in the third select would load the type of account: if it is savings or current and finally the account number of the employee.