Select dynamics in php and js by the POST method

0

The following code corresponds to the query and information obtained from the MySQL DB:

<?php
$conexion = new PDO("mysql:host=localhost;dbname=scouts_601_palmira","root","");

$Id = $_POST['Id'];

echo '<script type="text/javascript">alert("'.$Id.'");</script>';

$sql = "SELECT Id, Nombre FROM Especialidades WHERE Id_eje = ':$Id'";
$sentencia = $conexion -> prepare($sql);
$sentencia -> bindParam(':$Id', $Id, PDO::PARAM_STR);
$sentencia -> execute();
$resultado = $sentencia -> fetchAll(PDO::FETCH_ASSOC);

echo '<script type="text/javascript">alert("'.count($resultado).'");</script>';

$opcion = "<option value='0'>Seleccionelo</option>";

while ($especialidades = $resultado) {
$opcion = "<option value='".$fila['Id']."'>".$fila['Nombre']."</option>";
}

echo $opcion;
?>

This code does not return the query data.

And the following code divided into two parts is where the selections I require are, which are dynamic.

This first part contains the JavaScript that makes use of the code provided above.

<script type="text/javascript">
$(document).ready(function() {
    $("#eje-progresion").change(function() {
        //$("#nivel-especialidad").find('option').remove().end().append(
        //  '<option value="Seleccione"></option>').val('Seleccione');

        $("#eje-progresion option:selected").each(function() {
            Id = $(this).val();
            $.post("../Sql/ArregloEspecialidades.php", {Id: Id
            }, function(data){
                $("#especialidad-eje").html(data);
            });
        });
    })
});

This is the PHP and HTML code where the select are.

<form method="POST">
<table class="table table-bordered">
<thead>
    <tr>
        <td>Persona</td>
        <td>Eje</td>
        <td>Especialidad</td>
        <td>Nivel</td>
        <td>Acción</td>
    </tr>
</thead>
<tbody>
    <tr>
        <td><select>
            <option></option>
            <?php
            foreach ($personas_rama as $fila) {?>
                <option><?php echo $fila['Primer_nombre']." ".$fila['Segundo_nombre']." ".$fila['Primer_apellido']." ".$fila['Segundo_apellido']; ?></option>
            <?php
            }
            ?>
        </select></td>
        <td><select id="eje-progresion" name="eje-progresion">
            <option>Selecciona</option>
            <?php
            $ejes = DAOEje :: consultarNombreEjesPorIdProgresion($conexion, $id_cargo_recuperado);
            foreach ($ejes as $eje) {?>
                <option value="<?php echo $eje['Id']; ?>"><?php echo $eje['Nombre']; ?></option>
            <?php
            }
            ?>
        </select></td>
        <td><select id="especialidad-eje" name="especialidad-eje">
        </select></td>
        <td><select id="nivel-especialidad" name="nivel-especialidad">
            <option></option>
        </select></td>
        <td><button type="button" class="btn btn-success">Guardar</button></td>
    </tr>
</tbody>

I thank you in advance for any contribution that you have to solve or guidance to solve this problem.

    
asked by Danyel Melendez Ramirez 08.10.2018 в 18:44
source

1 answer

1

Try modifying your javascript code so it looks like this:

<script type="text/javascript">
    $(document).ready(function() {
        $("#eje-progresion").change(function() {    
            var opcionSeleccinada = $(this).val();
            if(opcionSeleccinada > 0){
                $.post("../Sql/ArregloEspecialidades.php", {Id: opcionSeleccinada
                }, function(data){
                    $("#especialidad-eje").html(data);
                });
            }
        })
    });

You are supposed to capture the selected element in the event onChange of the select and if it is different from 'Select an element' then you make the $.post and pass that id.

    
answered by 08.10.2018 / 23:06
source