<form>
<select id="cliente"></select>
<select id="usario"></select>
<select id="correo"></select>
<select id="telefono"></select>
</form>
$('#cliente').on('change', function(){
var id = $('#cliente').val()
$.ajax({
type: 'POST',
url: './usuario.php',
data: {'id': id}
})
.done(function(datos){
$('#usuario').html(datos)
})
.fail(function(){
alert('Hubo un errror al cargar los usuarios')
})
})
<?php
function getUsuario(){
require_once 'conexion.php';
$id = $_POST['id'];
$stmt = $conexion->prepare("SELECT id, nom_usuario FROM usuario WHERE id_cliente =:id_cliente");
$stmt->execute([':id_cliente' => $id]);
$usuario = '<option value="0">Elige un usuario</option>';
while(
$row = $stmt->fetch(PDO::FETCH_ASSOC)
){
$usuario .= "<option value='$row[id]'>$row[nombre]</option>";
}
return $usuario;
}
echo getUsuario();
//php
$row['nombre'] = '<option value="'.$row['id'].'">'.$row['nombre'].'</option>';
$row['correo'] = '<option value="'.$row['correo'].'">'.$row['correo'].'</option>';
$row['telf'] = '<option value="'.$row['telf'].'">'.$row['telf'].'</option>';
print json_decode($row);//todo el resultado
//ajax
$('#usario').html(datos.nombre);
$('#correo').html(datos.correo);
$('#telefono').html(datos.telf);
$stmt = $conexion->prepare("SELECT * FROM usuario WHERE id_cliente =:id_cliente");
$('#usuario').on('change', function(){
var id = $('#usuario').val()
$.ajax({
type: 'POST',
url: './datos.php',
data: {'id': id}
})
.done(function(datos){
$('#correo').val(datos.correo)
$('#telefono').val(datos.telefono)
})
.fail(function(){
alert('Hubo un errror al cargar los datos')
})
})
<?php
function getDatos(){
require_once 'conexion.php';
$id = $_POST['id'];
$stmt = $conexion->prepare("SELECT * FROM usuario WHERE id =:id");
$stmt->execute([':id' => $id]);
while(
$row = $stmt->fetch(PDO::FETCH_ASSOC)
){
$row['correo'] = '<option value="'.$row['correo'].'">'.$row['correo'].'</option>';
$row['telefono'] = '<option value="'.$row['telefono'].'">'.$row['telefono'].'</option>';
print json_decode($row);
}
return json_decode($row);
}
echo getDatos();
<form>
<select id="cliente"></select>
<select id="usario"></select>
<div id="DivCorreo"><select id="correo"></select></div>
<div id="DivTelefono"><select id="telefono"></select></div>
</form>
('#usuario').on('change', function(){
var id = $('#usuario').val()
$.ajax({
type: 'POST',
url: './correo.php',
data: {'id': id}
})
.done(function(datos){
$('#DivCorreo').html(datos)
})
.fail(function(){
alert('Hubo un errror al cargar los el correo')
})
$.ajax({
type: 'POST',
url: './Telefono.php',
data: {'id': id}
})
.done(function(datos){
$('#DivTelefono').html(datos)
})
.fail(function(){
alert('Hubo un errror al cargar el telefono')
})
})