PHP - Cargar un select o lista desplegable desde otro select

 
Vista:

Cargar un select o lista desplegable desde otro select

Publicado por KURRY (1 intervención) el 13/07/2016 23:20:54
lo primero de todo agradecer la gran ayuda desinteresada que prestan los usuarios del foro.Es la primera vez que posteo.Ssiempre busco y hay alguien que ha tenido el mismo problema y ya se lo han resuelto,pero esta vez no he encontrado respuesta.

a ver si planteo my duda bien:

quiero que los campos de un formulario se rellenen automaticamente según lo seleccionado en el campo previo,hay ejemplos..pero solo con dos selec,los que he encontrado con 3 no me funcionan..o no encuentro el fallo.
el archivo inicial undex.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
header('Content-Type: text/html; charset=ANSI');
?>
 
<html>
<head>
 
<title>Generador</title>
 
 
<link href="css/generador.css" rel="stylesheet" type="text/css"  />
 
 
<script src="ajax.js"></script>
 
 
</script>
</head>
 
<body>
 
 
 
<h1  >GENERADOR</h1>
 
<div id="txt_portada">
 
<p class="normal"> ve seleccionando.....
 
 
</p>
 
 
<?php
 
include 'conexion.php';
 
?>
 
<?php
 
// SQL query
	$sql = "SELECT * FROM capitulo";
 
 
	// Execute the query (the recordset $rs contains the result)
	$result = mysqli_query($con,$sql);
?>
 
 
<?php
$ntotal_capitulos=mysqli_num_rows($result);
 
 
echo"<p class='normal'>

con un total de $ntotal_capitulos capitulos</p>";
?>
 
<div id="lista_miembros_form">
 
 
<select  size=11 id="cont" onchange="load(this.value)">
 
 
    <?php
 
while (     $row = $result->fetch_array()  )
 
        {
   ?>
        <option lista=" <?php echo $row['capitulo'] ?> " >
        <?php echo $row['capitulo'] ;?>
 
 
        </option>
<?php
 
 
     }
 ?>
</select>
 
<div id="subcapitulo"></div>
<div id="partida"></div>
 
 
</div>
 
	</body>
</html>


el archivo ajax.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function load(str)
{
var xmlhttp;
 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("subcapitulo").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","subcapitulo.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+str);
};

el archivo subcapitulo.php

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
<?php
 
$q=$_POST['q'];
$con = mysqli_connect("127.0.0.1","root","","art");
 
 
	// SQL query
	$sql = "SELECT * FROM ".$q." ";
 
	// Execute the query (the recordset $rs contains the result)
 
 
$res = mysqli_query($con,$sql);
 
 
?>
 
<select id="subcapitulo" size=11 onchange="load2(this.value)">
 
 
    <?php
 
while (     $row = $res->fetch_array()  )
 
 
{ ?>
        <option lista=" <?php echo $row['subcapitulo'] ?> " >
        <?php echo $row['subcapitulo'] ;?>
 
 
        </option>
<?php } ?>
 
</select>

asi funcionan los dos prinmeros selec..pero evidentemente falla la funcion "load 2"..que no esta definida


he encontrado en algun sitio el siguiente codigo para un ajax alternativo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function load(string,url,cfunc)
{
var xmlhttp;
 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
{
 
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(string);
}

con estro se supone que defino dos funciones en el index tales como....

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
<script>
function subcapitulo(str)
{
load("q="+str,"subcapitulo.php",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("subcapitulo").innerHTML=xmlhttp.responseText;
    }
  });
}
 
 
function partida(str)
{
load("r="+str,"partida.php",function()
  {
 
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("partida").innerHTML=xmlhttp.responseText;
 
    }
  });
}
 
</script>

y deberia funcionar cambiando las funciones en el index.php de" load" por "subcapitulo" y en el subcapitulo .php la funcion de" load 2" por" partida"


pues no hace ni cascaras........alguna ayuda?

muchas gracias por adelantado
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