Good afternoon!
I have the following problem, I have to withdraw reports from a certain database where I should select specific employees, the same should 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>
When I select an employee, I want it to be 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>
'
and 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>";