PHP - Select Dependiente

 
Vista:

Select Dependiente

Publicado por ABL (1 intervención) el 02/10/2017 23:20:10
Buenas amigos, mi problema surge en el punto en que tengo 2 select dependientes, uno me muestra el departamento y el otro el municipio, al dar click sobre el departamento me muestra el municipio que corresponde pero el select del departamento vuelve al primer campo y no me muestra el departamento que seleccioné; ademas el primer departamento del listado no se deja seleccionar.

aqui el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
include("conect.php");
$conexion=mysqli_connect($host, $user, $pass);
if(mysqli_connect_errno()){
	echo "Falló la conexion";
	exit();
}
mysqli_select_db($conexion, $name) or die ("No se encuentra la base de datos");
 
$depto=$SESSION['depto2']=@$_REQUEST['depto'];
$muni=$SESSION['muni2']=@$_REQUEST['muni'];
 
//DEPARTAMENTO
$consulta="select * from depto";
$respuesta=mysqli_query($conexion, $consulta);
 
//MUNUCIPIO
$consulta2="select * from muni where iddepto=$depto order by muni ASC";
$respuesta1=mysqli_query($conexion, $consulta2);
 
?>
 
<!DOCTYPE html>
<html>
<head>
	<title>select dependiente</title>
</head>
<body>
	<form name="selectdin">
		<label>DEPARTAMENTO</label>
		<select class="form-control" name="depto" onchange="this.form.submit()">
 
			<?php
			while ($fila=mysqli_fetch_array($respuesta)) {
			?>
				<option value="<?php echo $fila['iddepto'] ?>"> <?php echo htmlentities($fila['depto']); ?>
				</option>";
			<?php
			}
			?>
		</select>

		<label>MUNICIPIO</label>
		<select class="form-control" name="muni">

			<?php
			while ($fila1=mysqli_fetch_array($respuesta1)) {
			?>
				<option value="<?php echo $fila1['idmuni'] ?>"> <?php echo htmlentities($fila1['muni']); ?>
				</option>";
			<?php
			}
			?>
		</select>
	</form>
</body>
</html>

La base de datos en MySQL se llama COLOMBIA y contiene 2 tablas: Depto y Muni.

Tabla: Depto
Campos: iddepto, depto

Tabla: muni
Campos idmuni, muni, iddepto


Espero me puedan colaborar.

De antemano Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder