Run MySQL query on onChange

2

I have a select that shows the departments:

<select name="dpto" id="dpto">
   <option value="3">ATLANTICO</option>
   <option value="12">CESAR</option>
</select>

What I need is that when selecting X department, make a query to the Database and show me the data in the following way:

<span class="tipoboletin">Tipo Boletin: <b><?php echo $B_Tipo_Boletin ?></b></span>

This is the MySQL query I have:

SELECT * FROM boletin WHERE Departamento = '" . $idDpto . "'"

Where $idDpto would be the value of the department you select.

    
asked by Jose Dario Correa 21.08.2018 в 21:21
source

1 answer

0

you could do it in the following way this is just an example there are many ways to do it this is one of the easiest example:

  <select name="dpto" id="dpto">
       <option value="3">ATLANTICO</option>
       <option value="12">CESAR</option>
    </select>


   <div id="resultados"></div><!--aqui se mostraran los resultados-->

js file

 $(document).ready(function(){

           $("#dpto").on("change",function(){

            var valorSelect=$(this).val()//obtenemos el valor seleccionado en una variable


        $.ajax({

            url:"callback.php",
            type:"POST",
            data:{ valorSelect:valorSelect},//enviamos lo seleccionado

            success:function(respuesta){
               console.log( respuesta)
             $("#resultados").html(respuesta);//en el div con el id respuestas mostramos los resultados del callback

            }

          })

          })

        })

callback.php

$valorSelect=$_POST["valorSelect"];//recojemos lo seleccionado

$query=mysqli_query($conn,"SELECT * FROM boletin WHERE Departamento = '" . $valorSelect . "'");

$row=mysqli_fetch_array($query);

echo...////aqui imprimes tu resultados para que ajax lo recoja

I hope your help is welcome!

    
answered by 21.08.2018 в 21:37