I need to capture the data of the select to do a mysql query with them and show the result in a table

0
 <?php
                include_once 'rutas.php';
                $host = "localhost";
                $user = "root";
                $pass = "";
                $bd = "freire";

                $con = mysqli_connect($host, $user, '', $bd) or die("No se ha podido conectar al servidor de Base de datos");
                $db = mysqli_select_db($con, $bd)or die("Upps! Pues va a ser que no se ha podido conectar a la base de datos");

                $query = "select * from conductores ";
                $result = mysqli_query($con, $query) or die("Algo ha ido mal en la consulta a la base de datos");

                $query1= "SELECT busid FROM conductores ";
                $result1 = mysqli_query($con, $query1) or die("Algo ha ido mal en la consulta a la base de datos");

                $query2= "SELECT origen FROM conductores ";
                $result2 = mysqli_query($con, $query2) or die("Algo ha ido mal en la consulta a la base de datos");

                $query3= "SELECT destino FROM conductores ";
                $result3 = mysqli_query($con, $query3) or die("Algo ha ido mal en la consulta a la base de datos");

                  //$sql = "select {$variable} from rio where fecha_muestra>={$fecha_muestra1} and fecha_muestra<={$fecha_muestra2}";
                $consulta= "SELECT ventaT,totalP,billetesV,ingresoT FROM tabla ";               
                $resultado = mysqli_query($con, $consulta) or die("Algo ha ido mal en la consulta a la base de datos");
                mysqli_close($con);
                ?>
                <?php 

                ?>
             <!--<form method="post" action="rutas.php">-->
                <div class="form-group" >
                    <div class="row">
                        <label>Conductor:</label>
                        <select name="conductor" id="conductor" class="form-group">
                            <option value="0">---seleccione---</option>
                             <?php
                                while ($lista = mysqli_fetch_assoc($result)){ ?>
                                      <option value = " <?php echo $lista['nombre']; ?> "><?php echo $lista['nombre']; ?></option> 

                                <?php } ?>

                        </select>    

                        <label>Número Bus :</label>
                        <select name="bus" id="bus" class="form-group">
                            <option value="0">---seleccione---</option>
                              <?php
                                while ($li = mysqli_fetch_assoc($result1)){  ?>
                                 <option value = " <?php echo $li['busid']; ?> "><?php echo $li['busid']; ?></option> 

                                <?php } ?>                      
                        </select>


                        <label> Parada Origen:</label>
                        <select name="origen" id="origen" class="form-group">
                            <option value="0">---seleccione---</option>
                                 <?php
                                while ($li = mysqli_fetch_assoc($result2)){  ?>
                                      <option value = " <?php echo $li['origen']; ?> "><?php echo $li['origen']; ?></option> 

                                <?php } ?>                   
                        </select>


                        <label>Parada Destino:</label>
                        <select name="destino" id="destino" class="form-group">
                            <option value="0">---seleccione---</option>

                            <?php                                                    
                                while ($li = mysqli_fetch_assoc($result3)){  ?>
                                    <option value = " <?php echo $li['destino']; ?> "><?php echo $li['destino']; ?></option> 

                                <?php } ?>                       
                        </select>
                    </div>

                    <script>
   function buscar() {
       var conductor = document.getElementById("conductor").value;
       var bus = document.getElementById("bus").value;
       var origen = document.getElementById("origen").value;
       var destino = document.getElementById("destino").value;
       alert(bus);
       alert(conductor);
       alert(origen);
       alert(destino);

}
</script>
                    <button type="button" name="buscar"  onclick="buscar()" class="btn btn-info">Buscar <span class="glyphicon glyphicon-search"></span></button> 


                    <button type="button" class="btn btn-success" id="btnExport" value=" Export Excel " >Export Excel <span class="glyphicon glyphicon-download-alt"></span></button>


                </div>                    


                <div class="panel-body">
                    <div id="dvData" class="table-responsive">
                        <table id="exportTable" class="table table-striped table-bordered table-hover">

                            <thead>
                                <tr class="danger">
                                    <th>Venta Taquilla</th>
                                    <th>Total Pasajeros</th>
                                    <th>Billetes Vendidos </th>
                                    <th>Ingreso Total</th>                                        
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <?php
                                    while ($datos = $resultado->fetch_array()) {
                                        ?>
                                        <tr>
                                            <td><?php echo $datos["ventaT"] ?></td>
                                            <td><?php echo $datos["totalP"] ?></td>
                                            <td><?php echo $datos["billetesV"] ?></td>
                                            <td><?php echo $datos["ingresoT"] ?></td>

                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tr>

                            </tbody>
                        </table>
                    </div>
                </div>

            </div>
             <footer><p>Copyright © 2017: <a href="http://empresafreire.com">Empresa Freire S.L.</a></p></footer>
        </div>

    </div>
    
asked by enzo 10.11.2017 в 23:31
source

1 answer

1

I do not see any problem in your code, except that you have commented on the form element, which could be problematic if you want to send the data contained in a form that is not such.

Anyway I write this answer to indicate some things:

  • Whenever you are going to manipulate DOM elements, refer to them from a function that evaluates the state of DOMContentLoaded ... If you try to use an element without the DOM finishes loading, you will not find that element and the code it will fail.
  • I would not put the function inside the button, but it would give an id to the button and I would listen to the clicks of that button in JS through its id. That way your HTML is a little more independent.
  • The code should work. Here is a test.

    /*Se recomienda llamar  esta función  
     *para asegurar que los elementos del DOM están disponibles
     *dentro de ella se deben manipular todos los elementos del DOM*/
     
    document.addEventListener("DOMContentLoaded", function(event) {
    
     var btnBuscar = document.getElementById("btnBuscar");
            btnBuscar.onclick  = buscar;
    
    });
    
    
    function buscar() {
    /*
      var conductor = document.getElementById("conductor").value;
      var bus = document.getElementById("bus").value;
      var destino = document.getElementById("destino").value;
      alert(bus);
      alert(conductor);
      alert(destino);
    */
    
      var origen = document.getElementById("idorigen").value;
      alert("Seleccionaste el origen: "+origen);
    
    }
    <form id="frm" method="post" action="rutas.php">
      <label> Parada Origen:</label>
      <select name="origen" id="idorigen" class="form-group">
          <option value="0">---seleccione---</option>
          <option value="1">Origen 1</option>
          <option value="2">Origen 2</option>
          <option value="3">Origen 3</option>
          </select>
      <button id="btnBuscar">Buscar</button>
    </form>
        
    answered by 11.11.2017 в 03:40