To do this use Javascript
, or better yet, a javascript library called j-query
, that greatly facilitates the requisitions Ajax
, example...
form
# Jquery #
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
# Requisição ajax #
<script src="model/ajaxCli.js"></script>
<form name="formCurl" id="env" method="post">
<label for="inpPesquisa">Pesquise por clientes</label>
<input type="text" placeholder="Digite o nome" name="termo" required="required">
<input type="submit">
</form>
ajaxCli.js
$(document).ready(function(){
$( "#env" ).submit(function( event ) {
event.preventDefault();
# pega os dados do formulário #
var data = $("#env").serialize();
$.ajax({
type : "POST",
# página php onde irá consultar #
url : "controller/clientes.php",
data : data,
dataType: "json",
success : function(response){
# em caso de sucesso #
if (response.codigo == 1) {
$('tbody').html('<tr><td>' + response.nomeCli + '</td><td>' + response.nomeCat + '</td></tr>');
};
# em caso de cliente não encontrado #
if (response.codigo == 0) {
alert('Cliente não encontrado');
};
}
})
});
})
php clients.
<?php
class getClientes{
# seu método # ...
if(#sua condição#) {
$retorno = array('codigo' => 1);
echo json_encode($retorno);
exit();
}
else {
$retorno = array('codigo' => 0);
echo json_encode($retorno);
exit();
}
}
$execClass = new getClientes();
$execClass->termo = (isset($_POST['termo']) ? $_POST['termo'] : '');
$execClass->_getClientes($execClass->termo);
?>
Basically that’s it, just study this function more. By the way, since it comes from desktop, you can change the library J-query
by a more modularized, example AngularJs
, more this for medium/long term projects, because the learning curve is higher
If you use jQuery, just put inside the file that Ajax will call a script filling the fields NAME and CITY if they are found; if not, send an Alert.
– Sam