I'm developing a system, and one of the fields, it will fetch the contract record if the user types 3 characters and after that, it will populate two other fields automatically.
The first part of Search is OK, but the second part that is related to fetching the other records is not working. In case I am using PHP and Jquery.
Here are the snippets of the code I'm using
<script type="text/javascript">
//Cria a função que buscará o autocomplete
$(document).ready(function() {
//Captura o retorno do buscarContrato.php
$.getJSON('buscarContrato.php',function(data) {
var num_contrato = [];
//Armanzena na array capturando somente o número do contrato
$(data).each(function(key,value) {
num_contrato.push(value.num_contrato);
});
//Chama o auto complete do Jquery
$('#Contrato').autocomplete({source: num_contrato, minLength: 3 });
});
});
</script>
This first excerpt is OK, it reads the file fetch.contract.php and brings the contract in when typing it in the HTML field. Below the PHP file
<?php
/*********************************************************************************************************/
/***Arquivo buscarContrato.php. Utilizado para efetuar a busca de contrato e completar automaticamente **/
/*******************************************************************************************************/
//Inclui a Conexao com o Banco
include "../STRUTS/Conexao.php";
#Varíaveis globais que serão usadas para cadastramento
SQL();
global $sql;
//Cria a consulta que será utilizada
$consultaContrato = "SELECT num_contrato FROM [REPORT].[dbo].[report_contratos]";
//Inicia o tratamento da consulta
$listaContratos = $sql->prepare($consultaContrato);
$listaContratos->execute(); //Executa a consulta
echo json_encode($listaContratos->fetchAll(PDO::FETCH_ASSOC));
?>
The second part will be what will bring the two other fields in the case the Operator and the Manager of this contract. The idea is that you choose the contract, the other two fields fill in automatically, but they are not coming. Below is the code snippet in Jquery and the PHP file
$(document).ready(function() {
$("#Contrato").blur(function() {
//Criando as varíaveis
var $operadora = $("#Operadora");
var $gestora_contrato = $("#Gestora");
var num_contrato = $(this).val();
$.getJSON('processa_contrato.php',(num_contrato),
function(retorno) {
$operadora.val(retorno.operadora);
$gestora_contrato.val(retorno.gestora_contrato);
});
});
});
And now the part in PHP.
<?php
/************************************************************************************************************/
/** Arquivo processa_contrato.php. Utilizado para efetuar a busca de contrato e completar automaticamente **/
/**********************************************************************************************************/
//Inclui a Conexao com o Banco
include "../STRUTS/Conexao.php";
#Varíaveis globais que serão usadas para cadastramento
SQL();
global $sql;
$num_contrato = filter_input(INPUT_GET, 'contrato', FILTER_SANITIZE_STRING);
if(!empty($contrato)) {
$limit = 1;
$buscaDados = "SELECT num_contrato,operadora,gestora_contrato FROM [REPORT].[dbo].[report_contratos]
WHERE num_contrato = :num_contrato LIMIT :limit";
$resultDados = $sql->prepare($buscaDados); //Prepara a consulta
$resultDados->bindParam(':num_contrato',$contrato,PDO::PARAM_STR);
$resultDados->bindParam(':limit',$limit,PDO::PARAM_INT);
$resultDados->execute(); //Executa a consulta
$array_valores = array();
if($resultDados->rowCount() != 0) {
$row_contrato = $resultDados->fetch(PDO::FETCH_ASSOC);
$array_valores['operadora'] = $row_contrato['operadora'];
$array_valores['gestora_contrato'] = $row_contrato['gestora_contrato'];
} else {
$array_valores['operadora'] = 'CONTRATO INEXISTENTE !!!';
}
echo json_encode($array_valores);
}
?>
What could be done so that when typing the contract, the other two fields are filled in automatically? Thanks!