Well, I'm trying to use Json to fill out a form with some data, the problem is that I have tried several forms and it still does not work and I already have it here is a big mess. / p>
Second, you need a javascript function to connect between the client and the server.
Thirdly you need a page that will do the research in the database through the parameters that are sent.
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xh1tml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Oficina Marcelo - Gestão de clientes</title>
<!-- Bootstrap Core CSS -->
<link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="dist/css/sb-admin-2.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- <script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>-->
<script src="bower_components/metisMenu/dist/metisMenu.min.js"></script>
<script src="dist/js/sb-admin-2.js"></script>
<!-- Autocomplete -->
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type='text/javascript' src='psqMat.js'></script>
<script>
$(document).ready(function(){
$("#ref").autocomplete("autocomplete.php", {
selectFirst: true
});
});
</script>
<!-- Fim Autocomplete -->
<script type="text/javascript">
function getRef(o) {
var ref = o.value;
//alert("a referencia a pesquisar é: " + ref); //Ate aqui chega e passa o valor da text
$.ajax({
url: "psqMat.php",
dataType: 'json',
type:"POST",
data: "?ref=" + ref,
success: function(response) {
$("#designacao").val(response.designacao);
if (response.marca !== undefined ) {
$("#marca").val(response.marca);
}
if (response.uniMed !== undefined ) {
$("#unimed").val(response.uniMed);
}
$("#precoUnit").val(response.precoUnit);
if (response.iva !== undefined ) {
$("#iva").val(response.iva);
}
},
// error: function (textStatus, errorThrown) {
// alert("shit happened: " + textStatus);
}
});
}
</script>
<!--<script type="text/javascript" src="func.js"></script>-->
</head>
<body>
[/code]
psqMat.php :
<?php
include 'includes/conexao.php';
include 'includes/validaLogin.php';
$ref = $_GET['ref'];
$sql = "SELECT ref, designacao, ID_marca, ID_unimed, precoUnit, ID_iva FROM tb_material WHERE ref=".$ref;
$result = mysqli_query($sql);
// fazer queries à BD com a referencia se existir 1 e só 1 resultado vamos criar a variavel $res / com a dados necessarios (a partir do resultado do query)
if ($r = mysqli_fetch_assoc($result)){
$res = [
"ref" => $r['ref'],
"designacao" => $r['designacao'],
"marca" => $r['ID_marca'],
"unimed" => $r['ID_unimed'],
"precoUnit" => $r['precoUnit'],
"iva" => $r['ID_iva']
];
}// devolver o resultado
echo json_encode($res);
?>
[/code]
e ainda existe o ficheiro psqMat.js (o que nao sei qual é a função dele, pois ja nao sei se ainda é necessário ou nao...)
[code=javascript]
$(document).ready( function() {
/* Executa a requisição quando o campo CEP perder o foco */
$('#ref').blur(function(){
alert("Chegou " + $("#ref").value);
/* Configura a requisição AJAX */
$.ajax({
url : 'psqMat.php', /* URL que será chamada */
type : 'POST', /* Tipo da requisição */
data: 'ref=' + $('#ref').val(), /* dado que será enviado via POST */
dataType: 'json', /* Tipo de transmissão */
success: function(data){
if(data.sucesso == 1){
$('#designacao').val(data.designacao);
$('#marca').val(data.marca);
$('#unimed').val(data.uniMed);
$('#precoUnit').val(data.precoUnit);
$('#iva').val(data.iva);
$('#qtd').focus();
}
}
});
return false;
})
});
Is everything correct or not yet?