Te paso nuevamente los codigos
Formulario
Código PHP:
<?php
require_once("funciones.php");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Formulario con SELECT > OPTION Dinamico</title>
<script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<form style="width: 480px">
<fieldset>
<legend>Seleccione su entidad federativa</legend>
<label>Linea Aerea:</label>
<select name="cia" id="cia">
<option value="">Seleccione una Linea Aerea</option>
<?php
$cia = dameCia();
foreach($cia as $indice => $registro){
echo "<option value=".$registro['cia'].">".$registro['aerolinea']."</option>";
}
?>
</select>
<br><br>
<label>Tarjeta:</label>
<select name="tc" id="tc">
<option value="">Primero seleccione una Linea Aerea</option>
</select>
<br><br>
<label>Banco:</label>
<select name="bco" id="bco">
<option value="">Primero seleccione una Tarjeta</option>
</select>
<br><br>
<label>Cuotas:</label>
<select name="cta" id="cta">
<option value="">Primero seleccione un Banco</option>
</select>
</fieldset>
</form>
<hr>
<script>
$("#cia").on("change", buscarTc);
$("#tc").on("change", buscarBco);
$("#bco").on("change", buscarCta);
function buscarTc(){
$cia = $("#cia").val();
if($cia == ""){
$("#tc").html("<option value=''>Primero seleccione una Linea Aerea</option>");
} else {
$.ajax({
dataType: "json",
data: {"cia": $cia},
url: 'buscar.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
$("#tc").html(respuesta.html);
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\n \n responseText: "+xhr.responseText);
}
});
}
}
function buscarBco(){
$tc = $("#tc").val();
$cia = $("#cia").val();
if($tc == ""){
$("#bco").html("<option value=''>Primero seleccione una Tarjeta</option>");
} else {
$.ajax({
dataType: "json",
data: {"tc": $tc, "cia": $cia},
url: 'buscar.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
$("#bco").html(respuesta.html);
},
error:function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\n \n responseText: "+xhr.responseText);
}
});
}
}
function buscarCta(){
$bco = $("#bco").val();
if($bco == ""){
$("#cta").html("<option value=''>Primero seleccione un Banco</option>");
} else {
$.ajax({
dataType: "json",
data: {"bco": $bco},
url: 'buscar.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
$("#cta").html(respuesta.html);
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\n \n responseText: "+xhr.responseText);
}
});
}
}
</script>
</body>
</html>
Funciones.php
Código PHP:
<?php
/* Archivo para funciones */
function conectaBaseDatos(){
try{
$servidor = "localhost";
$basedatos = "info_cias";
$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 dameCia(){
$resultado = false;
$consulta = "SELECT DISTINCT cia,aerolinea FROM coeficientes WHERE aerolinea <> 'tije' ORDER BY aerolinea ASC";
$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 dameTc($cia = ''){
$resultado = false;
$consulta = "SELECT DISTINCT tarjeta,nombre FROM coeficientes";
if($cia != ''){
$consulta .= " WHERE cia = :cia";
}
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
$sentencia->bindParam('cia',$cia);
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 dameBco($tc = '', $cia = ''){
$resultado = false;
$consulta = "SELECT DISTINCT banco FROM coeficientes";
if($tc != ''){
$consulta .= " WHERE tarjeta = :tc AND cia = :cia";
}
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
$sentencia->bindParam('tc',$tc);
$sentencia->bindParam('cia',$cia);
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 dameCta($bco = ''){
$resultado = false;
$consulta = "SELECT DISTINCT cuotas FROM coeficientes";
if($bco != ''){
$consulta .= " WHERE banco = :bco AND cia = :cia AND tarjeta = :tc ORDER BY coutas ASC";
}
$conexion = conectaBaseDatos();
$sentencia = $conexion->prepare($consulta);
$sentencia->bindParam('bco',$bco);
$sentencia->bindParam('tc',$tc);
$sentencia->bindParam('cia',$cia);
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;
}
?>
BUSCAR.PHP
Código PHP:
<?php
require_once("funciones.php");
if(isset($_POST['cia'])){
$tc = dameTc($_POST['cia']);
$html = "<option value=''>Seleccione una Tarjeta</option>";
foreach($tc as $indice => $registro){
$html .= "<option value='".$registro['tarjeta']."'>".$registro['nombre']."</option>";
}
$respuesta = array("html"=>$html);
echo json_encode($respuesta);
}
if(isset($_POST['tc'])){
$bco= dameBco($_POST['tc'], $_POST['cia']);
$html = "<option value=''>Seleccione un Banco</option>";
foreach($bco as $indice => $registro){
$html .= "<option value='".$registro['banco']."'>".$registro['banco']."</option>";
}
$respuesta = array("html"=>$html);
echo json_encode($respuesta);
}
if(isset($_POST['bco'])){
$cta= dameCta($_POST['bco']);
$html = "<option value=''>Seleccione cantidad de cuotas</option>";
foreach($cta as $indice => $registro){
$html .= "<option value='".$registro['cuotas']."'>".$registro['cuotas']."</option>";
}
$respuesta = array("html"=>$html);
echo json_encode($respuesta);
}
?>
Saludos