Dependent php combos postgres

0

Friends, I'm working with html, php and postgres. I have two tables in my base

and with the following code I pretend to fill two combos, that in the first one shows the groups of animals (GRUPO table) and depending on the group that is chosen, the second combo is loaded with the corresponding animals of that group (ANIMALS table) .

    <?php
$host = "localhost";
$dbname = "animales";
$user = "postgres";
$pw = "admin";
$port = "5432";
$packedString = "host=" . $host . " dbname=" . $dbname . " user=" . $user . " password=" . $pw . " port=" . $port;
$db = pg_connect("$packedString")or die('Could not connect: ' . pg_last_error());
?>
//***** combos_combinados.php *****//
<html>
  <head>
    <script language="javascript">
      // Función que rellena el segundo combo según el valor seleccionado en el primero.
      // Se le pasa como parámetro el nombre del formulario desde el cuál se llama a la función
      function rellenaCombo(formulario)
      {
        with (document.forms[formulario])  // Establecemos por defecto el nombre formulario pasado para toda la función.
        {
          var cat = id_grupo[id_grupo.selectedIndex].value; // Valor seleccionado en el primer combo.
          var n = id_animal.length;  // Numero de líneas del segundo combo.

          id_animal.disabled = false;  // Activamos el segundo combo.

          for (var i = 0; i < n; ++i)
            id_animal.remove(id_animal.options[i]); // Eliminamos todas las líneas del segundo combo.

          id_animal[0] = new Option("Seleccione un animal", 'null'); // Creamos la primera línea del segundo combo.

          if (cat != 'null')  // Si el valor del primer combo es distinto de 'null'.
          {
   <?php
    // Para cada grupo, construimos el segundo combo con los animales del mismo.
    $cons_cat = @pg_exec($db, "SELECT * FROM grupo;");

    for ($l = 0; $l < pg_numrows($cons_cat); ++$l)
    {
     $cen = @pg_fetch_object($cons_cat, $l);
   ?>
            if (cat == '<?php echo $cat->id_grupo;?>')
            {
    <?php
          // Construimos los valores del segundo combo con los animales de grupo
      $cons_rez = @pg_exec($db, "SELECT * FROM animal WHERE id_grupo = ".$cat->id_grupo." ORDER BY nombre_animal;");

      for ($m = 0; $m < pg_numrows($cons_rez); ++$m)
      {
       $rez = @pg_fetch_object($cons_rez, $m);
    ?>
              id_animal[id_animal.length] = new Option("<?php echo $rez->nombre_animal;?>", '<?php echo $rez->id_grupo;?>');
    <?php
      }
    ?>
            }
   <?php
    }
   ?>
            Id_animal.focus();  // Enviamos el foco al segundo combo.
          }
          else  // El valor del primer combo es 'null'.
          {
            Id_animal.disabled = true;  // Desactivamos el segundo combo (que estará vacío).
            id_grupo.focus();  // Enviamos el foco al primer combo.
          }

          id_animal.selectedIndex = 0;  // Seleccionamos el primer valor del segundo combo ('null').
        }
      }
    </script>
  </head>
  <body>
    <table>
      <tr>
        <td>
        <form name="datos" method="post" action="...">

            <select name="id_grupo" onChange="rellenaCombo('datos');">
              <option value="null" selected>Seleccione un grupo de animales
<?php
// CODIGO PHP
 // Contruimos el primer combo con los valores de la tabla 'grupo'.
 $cons_cat = @pg_exec($db, "SELECT * FROM grupo;");

 for ($k = 0; $k < pg_numrows($cons_cat); ++$k)
 {
  $cat = @pg_fetch_object($cons_cat, $k);
  echo "              <option value=\"".$cat->id_grupo."\">".$cat->grupo."\n";
 }
?>
          </select>
        </td>
        <td>
          <select name="id_animal" disabled>
            <option value="null">Seleccione un animal
          </select>
        </td>
        </form>

      </tr>

    </table>
  </body>
</html>

But, I only load the first combo and it stays there

the console throws me these errors:

    
asked by IndiraRivas 20.03.2018 в 19:09
source

0 answers