php – Insert data from a combobox into a SELECT

Question:

Good evening!

I have the following problem, I have to pull reports from a certain database where I must select specific employees, they must be selected from a combobox:

<select name="usuario" id="usuario">
    <option value="" disabled="disabled" selected="selected">Selecione o Usuário</option>
    <option>funcionario 1</option>
    <option>funcionario 2</option>
    <option>funcionario 3</option>
    </select>

I want that when selecting an employee, it is sent to the following query:

($result = $mysqli->query("SELECT * FROM tabela WHERE usuario='$funcionario';"))

should i do this with json? thanks in advance.

Updating:

$(document).ready(function(){
    $("#usuario").on("change", function(){
        $.ajax({
            url        : 'sql.php',
            type       : 'POST',
            cache      : false,
            dataType   : 'json',
            data       : { 'usuario' : $("#usuario").val() },
            error      : function(){
                alert('Erro ao tentar a ação!');
            },
            success    : function( dados ){ 
                console.log(dados);
            }
        });
    });
}); 
<select name="usuario" id="usuario">
    <option value="" disabled="disabled" selected="selected">Selecione o Usuário</option>
    <option>funcionario 1</option>
    <option>funcionario 2</option>
    <option>funcionario 3</option>
    </select>

`

e o SQL:

$usuario = array("usuario" => array(array("codigo"=>"funcionario 1", "nome"=>"funcionario 1"), array("codigo" => "funcionario 2", "nome"=>"funcionario 2"), array("codigo"=>"funcionario 3","nome"=>"funcionario 3")));  

                    echo json_encode($usuario);

                    if ($result = $mysqli->query("SELECT * FROM tabela WHERE usuario='$usuario';"))
                    {
                            // exibir os registros se há registros para mostrar
                            if ($result->num_rows > 0)
                            {
                                    // exibir registros em uma tabela

                                    echo "<table border='1' cellpadding='5' cellspacing=0 style=border-collapse: collapse bordercolor='#4B5AAB'>";


                                    // definir cabeçalhos de tabela
                                   echo "<tr> <th>ID</th> <th>Data</th> <th>Hora de entrada</th> <th>Hora de saída</th> <th>Comentário</th>";



                                    while ($row = $result->fetch_object())
                                    {
                                            // cria uma linha para cada registro
                                            echo "<tr>";
                                            echo "<td>" . utf8_encode ($row->id) . "</td>";
                                            echo "<td>" . utf8_encode ($row->data) . "</td>";
                                            echo "<td>" . utf8_encode ($row->hora_entra) . "</td>";
                                            echo "<td>" . utf8_encode ($row->hora_saida) . "</td>";
                                            echo "<td>" . utf8_encode ($row->coment) . "</td>"; 
                                            echo "</tr>";
                                    }

                                    echo "</table>";

Answer:

Thiago the HTML is wrong, in the <option> tags change the idfuncionario attribute to value .

JQUERY

$(document).ready(function(){
$("#usuario").on("change", function(){
    $.ajax({
        url        : 'caminho_para_arquivo.php',
        type       : 'POST',
        dataType   : 'json',
        data       : ({ 'usuario' : $("#usuario").val() }),
        error      : function(){
            alert('Erro ao tentar a acao!');
        },
        success    : function( dados ){ 
            alert(dados);
        }
    });
});
});

and in the .php file you put something + or – like this:

SQL

$conn = mysql_connect('localhost', 'root', 'vertrigo') or die('Falha na conexão!');  
mysql_select_db('novo');
$objDados = mysql_query('select codigo from usuario where codigo = "69";', $conn);      
$arr = array();
while($row = mysql_fetch_array($objDados) ){
   array_push($arr, $row['codigo']); //isso inseri dados no final de um array.
}

print json_encode($arr);
Scroll to Top