How do I generate the SQL query with a select parameter in php?

0

Good as I can do to bring me a data from a table, by means of the query sql with a parameter, the parameter will be the value of a select

My code is:

and where the query is:

where is the N ° 1 is where the value obtained from the select goes by means of a variable. And in No. 2 is the result of the query that will be shown in an input.

YOU CAN HELP ME PLEASE IN THIS; HOW CAN I GET THE RESULT I LOOK FOR?.

THANKS

    
asked by Juan Gallego 17.04.2018 в 02:12
source

1 answer

0

You must use javascript onchange method I leave you an example: html:

    <div>Selecciona Estado : <select name="cbx_estado" id="cbx_estado">
        <option value="0">Seleccionar Estado</option>
        <?php while($rowE = $resultadoE->fetch_assoc()) { ?>
            <option value="<?php echo $rowE['id_estado']; ?>" <?php if($rowE['id_estado']==$estado) { echo 'selected'; } ?>><?php echo $rowE['estado']; ?></option>
        <?php } ?>
    </select></div>
    <div id="resultado"></div>

javascript:

    $(document).ready(function(){
        $("#cbx_estado").change(function () {



            $("#cbx_estado option:selected").each(function () {
                id_estado = $(this).val();
                $.post("getMunicipio.php", { id_estado: id_estado },      function(data){
                    $("#resultado").html(data);
                });            
            });
        })
    });

php where you receive the id of the select

$id_estado = $_POST['id_estado'];

$queryM = "SELECT id_municipio, municipio FROM t_municipio WHERE id_estado = '$id_estado' ORDER BY municipio";
$resultadoM = $mysqli->query($queryM);

$html= "<option value='0'>Seleccionar Municipio</option>";

while($rowM = $resultadoM->fetch_assoc())
{
    $html.= "<option value='".$rowM['id_municipio']."'>".$rowM['municipio']."</option>";
}

echo $html;

that is an example .. I hope it serves you!

    
answered by 17.04.2018 в 23:10