Store JavaScript variable within a PHP variable

0

The next problem is that when a person selects an option of a select, the value of that chosen option is obtained and stored within a php variable.

Here I get the Id of the selected option.

It occurs to me, for example, to use done or success in js when successfully passing the variable from js to php within the same page, to execute the PHP code below but I do not know how it would be .

<script>
$(document).ready(function() {
$("#nivel-especialidad").change(function() {
  $("#nivel-especialidad option:selected").each(function ejecutar() {
    Id_nivel = $(this).val();
    $.post(Id_nivel);
    //OBTENER EL ID DE LA OPCION SELECCIONADA
    //ASIGNANDOLO A UNA VARIABLE PHP MAS ABAJO
      $("#requisitos-especialidad").load();
     // ESTE CODIGO PIENSO QUE IRIA DENTRO DEL SUCCESS O DONE DE JS
     // PARA PODER EJECUTAR EL CODIGO PHP DE ABAJO
     // QUE ESTA DENTRO DE UN DIV
    });
  })
});
</script>

The following PHP code corresponds to the one I want to execute after the PHP variable gets the ID_NIVEL:

<?php
$id_nivel = $_POST['Id_nivel'];
            //AQUI GUARDARIA EL VAR ID_NIVEL DE JS PARA PASARLO EN LA SIGUIENTE LINEA
    ?>

<table class="table table-bordered">
<tbody>
    <div id="requisitos-nivel" name="requisitos-nivel">
    <?php
//ESTE ES EL CODIGO QUE QUIERO EJECUTAR DE FORMA DINAMICA CUANDO SE OBTENGA EL ID_NIVEL DENTRO DE LA VARIABLE PHP
$requisitos = DAORequisito :: consultarRequisitosPorIdNivel(Conexion :: obtenerConexion(), $id_nivel);//CONSULTA A LA BD

echo "<tr>";
for ($i = 0, $contador=1; $i < count($requisitos); $i++, $contador++) {?>
    <td>
    <div class="card" style="width: 20rem;">
            <!--<img class="card-img-top" src="..." alt="Card image cap">-->
            <div class="card-body">
                <p class="card-text"><?php
                echo $requisitos[$i] -> obtenerTexto();
                ?></p>
                <input type="checkbox">
            </div>
        </div>
    </td>
    <?php if(($contador%3) == 0) {
        echo "</tr><tr>";
    }
}
echo "</tr>";
?>
</div>
</tbody>
</table>
    
asked by Danyel Melendez Ramirez 11.10.2018 в 03:57
source

2 answers

0

I did not understand much of what you want to do but ... Do you try to get the value of a select and the id of a user?

You should use a input:hidden to send the id in the form, and in php use $_POST["id"] and $_POST["requisitos-nivel"] to get the id and the value selected in the select

    
answered by 11.10.2018 в 07:24
0

The intention of what you want to do is not very clear, but you have to keep in mind that Javascript is executed on the client side and PHP is executed on the server. With this in mind, you must use the HTTP POST method to perform this communication.

To send data from jQuery you can use $.post();

Check the doc here

And to receive them in PHP $_POST[] as indicated by Ale above

The doc here

    
answered by 11.10.2018 в 08:14